Skip to content

Instantly share code, notes, and snippets.

@moshfeu
Last active December 26, 2019 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moshfeu/38cf16db9138acc13c5118cac84e319a to your computer and use it in GitHub Desktop.
Save moshfeu/38cf16db9138acc13c5118cac84e319a to your computer and use it in GitHub Desktop.
AJV + Jest - gits #5
expect.extend({
toBeValid(isValid, errorMessage) {
return {
message: () => isValid ? '' : errorMessage,
pass: isValid
}
}
});
it(`should user's schema be valid`, () => {
const shema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"fullname": {
"type": "string",
"minLength": 2
},
},
},
};
const valid = ajv.validate(shema, [
{
fullname: 'a'
}
]);
try {
const [, index, fieldName] = /\[(.*)\].(.*)/.exec(error.dataPath);
return `error with item #${index}'s field "${fieldName}". The error is: ${error.message}`;
} catch (error) {
return error.message;
}
}).join('\n');
expect(valid).toBeValid(errorMessage);
});
@Efimenko
Copy link

Efimenko commented Dec 26, 2019

Hi. You missed const errorMessage = (ajv.errors || []).map(error => { after 28 line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment