Skip to content

Instantly share code, notes, and snippets.

@moshfeu
Created March 30, 2019 20:12
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/be993244199b1649f7ec31780071110d to your computer and use it in GitHub Desktop.
Save moshfeu/be993244199b1649f7ec31780071110d to your computer and use it in GitHub Desktop.
AJV + Jest - gits #6
const validateSecuredUrl = function (schema, uri) {
return uri.indexOf('https://') === 0;
};
ajv.addKeyword('securedUrl', {
validate: validateSecuredUrl,
errors: true
});
it(`should user's schema be valid`, () => {
const shema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"fullname": {
"type": "string",
"minLength": 2
},
"avatar": {
"type": "string",
"format": "uri",
"securedUrl": true,
},
},
},
};
const valid = ajv.validate(shema, [
{
fullname: 'ab',
avatar: "http://mywebsite.com/path/to/avatar"
}
]);
const errorMessage = (ajv.errors || []).map(error => {
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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment