Created
March 30, 2019 20:12
-
-
Save moshfeu/be993244199b1649f7ec31780071110d to your computer and use it in GitHub Desktop.
AJV + Jest - gits #6
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
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