fix: 修复 isJson 校验 (#2416)

Co-authored-by: wangfucheng <wangfucheng@machloop.com>
This commit is contained in:
Anonymity94 2021-08-20 20:12:58 +08:00 committed by GitHub
parent 78c6ea4abb
commit 99f4b040ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -465,6 +465,21 @@ test('validation:isJson invalid', () => {
).toMatchObject(['请检查 Json 格式']);
});
test('validation:isJson invalid', () => {
expect(
validate(
'12345',
{},
{
isJson: true
},
{
isJson: '请检查 Json 格式'
}
)
).toMatchObject(['请检查 Json 格式']);
});
test('validation:isLength valid', () => {
expect(
validate(

View File

@ -149,7 +149,11 @@ export const validations: {
isJson: function (values, value, minimum) {
if (isExisty(value) && !isEmpty(value) && typeof value === 'string') {
try {
JSON.parse(value);
const result = JSON.parse(value);
if (typeof result === 'object' && result) {
return true;
}
return false;
} catch (e) {
return false;
}