Skip to content

Commit 11b6665

Browse files
authored
fix: pass nested additionalProperty to validateSchema errors (#712)
1 parent eff4bdc commit 11b6665

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/hooks/validate-schema.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ function addNewErrorDflt(errorMessages: any, ajvError: any, itemsLen: any, index
8383
message = `'${leader}${ajvError.dataPath.substring(1)}' ${ajvError.message}`;
8484
} else {
8585
message = `${leader}${ajvError.message}`;
86-
if (ajvError.params && ajvError.params.additionalProperty) {
87-
message += `: '${ajvError.params.additionalProperty}'`;
88-
}
86+
}
87+
88+
if (ajvError.params && ajvError.params.additionalProperty) {
89+
message += `: '${ajvError.params.additionalProperty}'`;
8990
}
9091

9192
return (errorMessages || []).concat(message);

test/hooks/validate-schema.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ ajv.addSchema({
1111
properties: {
1212
first: { type: 'string', format: 'startWithJo' },
1313
last: { type: 'string' },
14+
nested: {
15+
type: 'object',
16+
additionalProperties: false,
17+
},
1418
},
1519
required: ['first', 'last'],
1620
});
@@ -55,13 +59,19 @@ describe('services validateSchema', () => {
5559
properties: {
5660
first: { type: 'string' },
5761
last: { type: 'string' },
62+
nested: {
63+
additionalProperties: false,
64+
},
5865
},
5966
required: ['first', 'last'],
6067
};
6168
schemaForAjvInstance = {
6269
properties: {
6370
first: { type: 'string', format: 'startWithJo' },
6471
last: { type: 'string' },
72+
nested: {
73+
additionalProperties: false,
74+
},
6575
},
6676
required: ['first', 'last'],
6777
};
@@ -161,6 +171,17 @@ describe('services validateSchema', () => {
161171
]);
162172
}
163173
});
174+
175+
it('properly displays incorrect additional properties in nested objects', () => {
176+
hookBefore.data.nested = { foo: 'bar' };
177+
178+
try {
179+
validateSchema(schemaForAjvInstance, ajv)(hookBefore);
180+
assert.fail(true, false, 'test succeeds unexpectedly');
181+
} catch (err: any) {
182+
assert.deepEqual(err.errors, ["'nested' should NOT have additional properties: 'foo'"]);
183+
}
184+
});
164185
});
165186

166187
describe('Async validation', () => {

0 commit comments

Comments
 (0)