Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ajv): undefined props validate structure error #457

Merged
merged 2 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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