fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

[FEATURE] At least one of

Open VityaSchel opened this issue 4 years ago • 2 comments

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.

VityaSchel avatar Jun 12 '21 10:06 VityaSchel

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'
}]

erfanium avatar Jun 12 '21 14:06 erfanium

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!

VityaSchel avatar Jun 12 '21 17:06 VityaSchel