Skip to content

Commit

Permalink
fix(ajv): undefined props validate structure error (#457)
Browse files Browse the repository at this point in the history
* fix(ajv): undefined props validate structure error

* test: update snapshot
  • Loading branch information
moonou committed Oct 20, 2022
1 parent 5fde07d commit 0f39c20
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
11 changes: 11 additions & 0 deletions ajv/src/__tests__/__fixtures__/data.ts
Expand Up @@ -60,6 +60,17 @@ export const invalidData = {
},
};

export const invalidDataWithUndefined = {
username: 'jsun969',
password: undefined,
deepObject: {
twoLayersDeep: {
name: 'deeper',
},
data: undefined,
}
}

export const fields: Record<InternalFieldName, Field['_f']> = {
username: {
ref: { name: 'username' },
Expand Down
22 changes: 22 additions & 0 deletions ajv/src/__tests__/__snapshots__/ajv.ts.snap
Expand Up @@ -54,6 +54,28 @@ Object {
}
`;

exports[`ajvResolver should return all the error messages from ajvResolver when some property is undefined and result will keep the input data structure 1`] = `
Object {
"errors": Object {
"deepObject": Object {
"data": Object {
"message": "must have required property 'data'",
"ref": undefined,
"type": "required",
},
},
"password": Object {
"message": "must have required property 'password'",
"ref": Object {
"name": "password",
},
"type": "required",
},
},
"values": Object {},
}
`;

exports[`ajvResolver should return all the error messages from ajvResolver when validation fails and validateAllFieldCriteria set to true 1`] = `
Object {
"errors": Object {
Expand Down
15 changes: 14 additions & 1 deletion ajv/src/__tests__/ajv.ts
@@ -1,5 +1,5 @@
import { ajvResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
import { fields, invalidData, invalidDataWithUndefined, schema, validData } from './__fixtures__/data';

const shouldUseNativeValidation = false;

Expand Down Expand Up @@ -81,4 +81,17 @@ describe('ajvResolver', () => {
}),
).toMatchSnapshot();
});

it('should return all the error messages from ajvResolver when some property is undefined and result will keep the input data structure', async () => {
expect(
await ajvResolver(schema, undefined, { mode: 'sync' })(
invalidDataWithUndefined,
undefined,
{
fields,
shouldUseNativeValidation,
},
),
).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion ajv/src/ajv.ts
Expand Up @@ -11,7 +11,7 @@ const parseErrorSchema = (
// Ajv will return empty instancePath when require error
ajvErrors.forEach((error) => {
if (error.keyword === 'required') {
error.instancePath = '/' + error.params.missingProperty;
error.instancePath += '/' + error.params.missingProperty;
}
});

Expand Down

0 comments on commit 0f39c20

Please sign in to comment.