Last active
December 26, 2019 17:30
-
-
Save moshfeu/38cf16db9138acc13c5118cac84e319a to your computer and use it in GitHub Desktop.
AJV + Jest - gits #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. You missed
const errorMessage = (ajv.errors || []).map(error => {
after 28 line.