Rule as array or string #4010
Unanswered
meteoguzhan
asked this question in
Help
Replies: 1 comment
-
Unfortunately, that's not currently possible with the Validator, but it is something that's been acknowledged as missing. In your case, however, you do have a couple of options. (I haven't tested either of the below, so they may not be exact, but should be close to working examples)
const stringSchema = schema.create({ value: schema.string.optional() }) // <- optional to bypass error if not provided
let data = await request.validate({ schema: stringSchema })
if (!data.value) {
const arraySchema = schema.create({ value: schema.array() })
data = await request.validate({ schema: arraySchema })
}
const value = data.value
// I'd put this portion in a service method
const body = request.body()
if (typeof body.value === 'string') {
body.value = [body.value] // convert to array
}
request.updateBody(body)
// end
// then you should be able to just validate for an array
const arraySchema = schema.create({ value: schema.array() })
const data = await request.validate({ schema: arraySchema })
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
How can I add rule as array or string in validation operation?
My current codes:
Beta Was this translation helpful? Give feedback.
All reactions