Merge pull request #5610 from brethubbard/master

Improve error messages when schema validation fails
This commit is contained in:
Ben Lambert
2021-05-10 09:31:46 +02:00
committed by GitHub
3 changed files with 24 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/catalog-model': patch
---
Improve error messages when schema validation fails
@@ -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(', ')}`
: ''
}`,
);
},
};