fastest-validator
fastest-validator copied to clipboard
[FEATURE] At least one of
I need validator that accepts multiple parameters and return true if at least one passed:
let schema = {
$$oneOf: [
token: { type: 'string' },
key: { type: 'string' }
]
}
So you can pass either token or key but you cannot validate object without both of them. There is $or operator but it only works for one parameter with multiple possible values.
const schema = {
token: 'string|optional',
key: 'string|optional',
$$custom(obj, errors) {
if (!obj.token && !obj.key) errors.push({ ... })
return obj
}
}
or Typescript's way:
const schema = [{
foo: 'string',
token: 'string'
}, {
foo: 'string',
key: 'string'
}]
const schema = { token: 'string|optional', key: 'string|optional', $$custom(obj, errors) { if (!obj.token && !obj.key) errors.push({ ... }) return obj } }or Typescript's way:
const schema = [{ foo: 'string', token: 'string' }, { foo: 'string', key: 'string' }]
thanks for this workaround, I'll try it!