How to use the when function from joi
Find comprehensive JavaScript joi.when code examples handpicked from public code repositorys.
joi.when is a conditional validation function in the Joi library that allows you to define validation rules based on the value of another field.
GitHub: ustaxcourt/ef-cms
158 159 160 161 162 163 164 165 166 167
PARTY_TYPES.partnershipAsTaxMattersPartner, PARTY_TYPES.partnershipBBA, PARTY_TYPES.partnershipOtherThanTaxMatters, ), otherwise: joi.optional().allow(null), then: joi.when('orderForCds', { is: joi.not(true), otherwise: joi.optional().allow(null), then: joi.required(), }),
+ 3 other calls in file
GitHub: ustaxcourt/ef-cms
714 715 716 717 718 719 720 721 722 723
.array() .items(Statistic.VALIDATION_RULES) .when('hasVerifiedIrsNotice', { is: true, otherwise: joi.optional(), then: joi.when('caseType', { is: CASE_TYPES_MAP.deficiency, otherwise: joi.optional(), then: joi.array().min(1).required(), }),
+ 7 other calls in file
How does joi.when work?
joi.when is a method in the Joi library that allows you to apply conditional validation to a schema by specifying a set of rules that will only be applied when certain conditions are met, such as the presence or absence of specific keys or their values in the input data. When defining a schema using joi.when, you specify a condition for the rule to apply, the action to take if the condition is true, and optionally, the action to take if the condition is false. The syntax for joi.when is joi.when(ref, options) where ref is a reference to another key in the schema and options is an object containing the condition, the then rule to apply if the condition is true, and the optional otherwise rule to apply if the condition is false.
54 55 56 57 58 59 60 61 62 63
).required(), docketNumber: JoiValidationConstants.DOCKET_NUMBER.required(), docketNumberWithSuffix: JoiValidationConstants.STRING, documentTitle: JoiValidationConstants.DOCUMENT_TITLE.required(), documentType: JoiValidationConstants.STRING, eventCode: joi.when('isSealed', { is: true, otherwise: JoiValidationConstants.STRING, then: JoiValidationConstants.STRING.valid( ...OPINION_EVENT_CODES_WITH_BENCH_OPINION,
68 69 70 71 72 73 74 75 76 77
// Url to Find Exporters app FIND_EXPORTERS_URL: Joi.string().uri(), // Force using an HTTPS connection FORCE_HTTPS: Joi.when('NODE_ENV', { is: 'production', then: Joi.bool().default(true), otherwise: Joi.bool().default(false), }),
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const Joi = require("joi"); const schema = Joi.object({ role: Joi.string().valid("admin", "user"), password: Joi.string().when("role", { is: "admin", then: Joi.string().min(8).required(), otherwise: Joi.string().min(6).required(), }), }); const data = { role: "admin", password: "1234", }; const result = schema.validate(data); console.log(result); // Output: { error: { ValidationError: "password" length must be at least 8 characters long }, value: { role: "admin", password: "1234" } }
In this example, we define a schema with two fields: role and password. The role field can have two possible values: 'admin' or 'user'. We use Joi.when() to conditionally apply validation rules to the password field based on the value of the role field. If the role is 'admin', we require the password to be at least 8 characters long. Otherwise, we require it to be at least 6 characters long. When we validate the data object against the schema, we get a validation error because the password field is too short for an 'admin' role.
joi.string is the most popular function in joi (40578 examples)