admin管理员组

文章数量:1296910

A classical example is:

schema = Joi.object().keys({
    my_string: Joi.string().valid("myString").required()
});

This validate that object have field my_string which must have a myString as value.

How check that key my_string is not equal to notAllowedString?

A classical example is:

schema = Joi.object().keys({
    my_string: Joi.string().valid("myString").required()
});

This validate that object have field my_string which must have a myString as value.

How check that key my_string is not equal to notAllowedString?

Share Improve this question edited Apr 26, 2019 at 7:32 Erich Kitzmueller 37k5 gold badges85 silver badges103 bronze badges asked Apr 26, 2019 at 6:36 CherryCherry 33.6k74 gold badges243 silver badges394 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

you can use invalid to blacklist a value (link for ref)

schema = Joi.object().keys({
    my_string: Joi.string().invalid("notAllowedString").required()
});

Here's a full example of how you would use it:

const Joi = require('joi');

const schema = Joi.object({
  someIntA: Joi.number().integer().min(0).required(),
  someIntB: Joi.number()
    .integer()
    .min(0)
    .invalid(Joi.ref('someIntA'))
    .required(),
  someStringA: Joi.string().alphanum().min(3).max(30).required(),
  someStringB: Joi.string()
    .alphanum()
    .min(3)
    .max(30)
    .invalid(Joi.ref('someStringA'))
    .required(),
});

本文标签: javascriptHow to validate that a string is not equal to another (blacklist) with JoiStack Overflow