Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <tsfarr@amazon.com>
  • Loading branch information
Xtansia committed Sep 9, 2024
1 parent f322d70 commit dec5cc6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/src/_utils/JsonSchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class JsonSchemaValidator {
try {
this.ajv.addSchema(schema, key);
} catch (e) {
throw new Error(`Failed to add schema ${key}: \`${JSON.stringify(schema)}\``, { cause: e })
throw new Error(`Failed to add schema ${key} (${JSON.stringify(schema)}):\n\t${e instanceof Error ? e.message : e as string}`)
}
})
this.errors_parser = new AjvErrorsParser(this.ajv, options.errors_text_opts)
Expand Down
13 changes: 13 additions & 0 deletions tools/tests/_utils/JsonSchemaValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ describe('JsonSchemaValidator', () => {

expect(validator.validate_schema(invalid_schema)).toEqual('data/required must be array');
});

test('constructing with invalid reference schema throws descriptive error', () => {
const invalid_options = {
reference_schemas: {
'#/components/schemas/Invalid': {
type: 'string',
required: true, // required must be an array
}
}
}
expect(() => new JsonSchemaValidator(schema, invalid_options))
.toThrow('Failed to add schema #/components/schemas/Invalid ({"type":"string","required":true}):\n\tschema is invalid: data/required must be array');
});
});

0 comments on commit dec5cc6

Please sign in to comment.