From 6ea07c26203dc86883338ad154aa59c015ee9d39 Mon Sep 17 00:00:00 2001 From: moonou Date: Thu, 20 Oct 2022 16:25:18 +0800 Subject: [PATCH 1/2] fix(ajv): undefined props validate structure error --- ajv/src/__tests__/__fixtures__/data.ts | 11 +++++++++++ ajv/src/__tests__/__snapshots__/ajv.ts.snap | 22 +++++++++++++++++++++ ajv/src/__tests__/ajv.ts | 15 +++++++++++++- ajv/src/ajv.ts | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ajv/src/__tests__/__fixtures__/data.ts b/ajv/src/__tests__/__fixtures__/data.ts index 79e91a9a..7e92fe65 100644 --- a/ajv/src/__tests__/__fixtures__/data.ts +++ b/ajv/src/__tests__/__fixtures__/data.ts @@ -60,6 +60,17 @@ export const invalidData = { }, }; +export const invalidDataWithUndefined = { + username: 'jsun969', + password: undefined, + deepObject: { + twoLayersDeep: { + name: 'deeper', + }, + data: undefined, + } +} + export const fields: Record = { username: { ref: { name: 'username' }, diff --git a/ajv/src/__tests__/__snapshots__/ajv.ts.snap b/ajv/src/__tests__/__snapshots__/ajv.ts.snap index 28e3c115..1819d327 100644 --- a/ajv/src/__tests__/__snapshots__/ajv.ts.snap +++ b/ajv/src/__tests__/__snapshots__/ajv.ts.snap @@ -54,6 +54,28 @@ Object { } `; +exports[`ajvResolver should return all the error messages from ajvResolver when some property is undefined and result will keep strcut 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 { diff --git a/ajv/src/__tests__/ajv.ts b/ajv/src/__tests__/ajv.ts index 1bfd7d5d..7c2f6108 100644 --- a/ajv/src/__tests__/ajv.ts +++ b/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; @@ -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(); + }); }); diff --git a/ajv/src/ajv.ts b/ajv/src/ajv.ts index b12d7e1b..9652a723 100644 --- a/ajv/src/ajv.ts +++ b/ajv/src/ajv.ts @@ -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; } }); From a61640d36e21fd747003f7ec8bed5c131028072b Mon Sep 17 00:00:00 2001 From: moonou Date: Thu, 20 Oct 2022 18:55:58 +0800 Subject: [PATCH 2/2] test: update snapshot --- ajv/src/__tests__/__snapshots__/ajv.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ajv/src/__tests__/__snapshots__/ajv.ts.snap b/ajv/src/__tests__/__snapshots__/ajv.ts.snap index 1819d327..7f8b8e4e 100644 --- a/ajv/src/__tests__/__snapshots__/ajv.ts.snap +++ b/ajv/src/__tests__/__snapshots__/ajv.ts.snap @@ -54,7 +54,7 @@ Object { } `; -exports[`ajvResolver should return all the error messages from ajvResolver when some property is undefined and result will keep strcut 1`] = ` +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 {