validate-by-example
Version:
Derives a JSON schema from an object and then uses it to validate other objects
28 lines (22 loc) • 530 B
JavaScript
const formats = require('is-my-json-valid/formats')
const is = require('check-more-types')
// only consider a subset of formats, otherwise matches weird stuff
const allowed = [
'date-time', 'date', 'time',
'email', 'ipv4', 'ipv6', 'uri',
'phone'
]
function findFormat (value) {
if (is.hexRgb(value)) {
return 'color'
}
return allowed.find(format => {
if (!formats[format]) {
return
}
const check = formats[format]
return check.test(value)
})
}
module.exports = findFormat