Improve error messages when schema validation fails

Signed-off-by: Bret Hubbard <hubbard.bret@gmail.com>
This commit is contained in:
Bret Hubbard
2021-05-06 16:07:27 -06:00
parent 67b16c4935
commit e9458540de
2 changed files with 19 additions and 1 deletions
@@ -168,4 +168,16 @@ components:
(entity as any).spec.system = '';
await expect(validator.check(entity)).rejects.toThrow(/system/);
});
it('rejects additional properties', async () => {
(entity as any).annotations = 'Test';
await expect(validator.check(entity)).rejects.toThrow(
/additional properties/,
);
});
it('rejects with useful error message', async () => {
(entity as any).annotations = 'Test';
await expect(validator.check(entity)).rejects.toThrow(/annotations/);
});
});
+7 -1
View File
@@ -66,7 +66,13 @@ export function ajvCompiledJsonSchemaValidator(
}
throw new TypeError(
`Malformed ${kind}, ${error.dataPath || '<root>'} ${error.message}`,
`Malformed ${kind}, ${error.dataPath || '<root>'} ${error.message}${
error.params
? ` - ${Object.entries(error.params)
.map(([key, val]) => `${key}: ${val}`)
.join(', ')}`
: ''
}`,
);
},
};