How to use the optionalId function from joi
Find comprehensive JavaScript joi.optionalId code examples handpicked from public code repositorys.
joi.optionalId is a function provided by the Joi library used for validating optional string identifiers that meet specified criteria.
923 924 925 926 927 928 929 930 931 932
typeIds: Joi.array().items(Joi.optionalId()), trackIds: Joi.array().items(Joi.optionalId()), types: Joi.array().items(Joi.string()), tracks: Joi.array().items(Joi.string()), typeId: Joi.optionalId(), trackId: Joi.optionalId(), type: Joi.string(), track: Joi.string(), name: Joi.string(), search: Joi.string(),
+ 97 other calls in file
91 92 93 94 95 96 97 98 99 100
isActive: Joi.boolean().required(), phases: Joi.array() .items( Joi.object().keys({ phaseId: Joi.id(), predecessor: Joi.optionalId(), defaultDuration: Joi.number().positive().required(), }) ) .min(1)
+ 5 other calls in file
How does joi.optionalId work?
joi.optionalId is a function provided by the Joi library for validating optional string identifiers that meet specified criteria. This function is often used to validate unique identifiers for database records, such as a MongoDB document ID. Under the hood, joi.optionalId returns a Joi schema object that includes rules for validating the identifier. The schema object can then be used to validate an object containing the identifier value, such as a database record. By default, joi.optionalId allows the identifier to be either a string or null. If the identifier is a string, it must meet the specified criteria, which can include a length limit, regular expression pattern, or other custom validation criteria. To use joi.optionalId, you pass the desired validation criteria as an options object to the function, such as {alphanum: true} to require that the identifier contain only alphanumeric characters. You can also specify additional validation rules, such as min or max to limit the length of the identifier. Overall, joi.optionalId provides a flexible and convenient way to validate optional string identifiers that meet specific criteria, helping to ensure that your application's data is well-formed and consistent.
30 31 32 33 34 35 36 37 38 39 40
searchChallengeTimelineTemplates.schema = { criteria: Joi.object().keys({ typeId: Joi.optionalId(), trackId: Joi.optionalId(), timelineTemplateId: Joi.optionalId(), isDefault: Joi.boolean() }) }
+ 2 other calls in file
150 151 152 153 154 155 156 157 158 159 160
searchGroups.schema = { isAdmin: Joi.boolean(), criteria: Joi.object().keys({ memberId: Joi.optionalId(), // defined in app-bootstrap universalUID: Joi.optionalId(), membershipType: Joi.string().valid(_.values(config.MEMBERSHIP_TYPES)), name: Joi.string(), page: Joi.page(), perPage: Joi.perPage(),
+ 19 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
const Joi = require("joi"); // Define a schema for a MongoDB document const schema = Joi.object({ _id: Joi.optionalId({ alphanum: true }).allow(null), name: Joi.string().required(), age: Joi.number().integer().min(0).max(120).required(), }); // Validate a document with an optional ID const doc1 = { _id: null, name: "John Doe", age: 30 }; const result1 = schema.validate(doc1); console.log(result1); // Output: { value: { _id: null, name: 'John Doe', age: 30 }, error: null } const doc2 = { _id: "1234abcd", name: "Jane Doe", age: 25 }; const result2 = schema.validate(doc2); console.log(result2); // Output: { value: { _id: '1234abcd', name: 'Jane Doe', age: 25 }, error: null } const doc3 = { _id: "foo!bar", name: "Bob Smith", age: 45 }; const result3 = schema.validate(doc3); console.log(result3); // Output: { value: null, error: [Error: "_id" must only contain alpha-numeric characters] }
In this example, a Joi schema is defined for a MongoDB document with an optional _id field, a required name field, and a required age field. The _id field is validated using Joi.optionalId, with the alphanum option specified to allow only alphanumeric characters. Three different documents are then validated using the schema. doc1 has a null _id field, which is allowed by the schema. doc2 has a valid string _id field, which is also allowed by the schema. doc3 has an invalid _id field containing a non-alphanumeric character, which fails validation and returns an error. Note that joi.optionalId is a flexible function that can be customized to meet the specific needs of your application, allowing you to validate unique identifiers according to your desired criteria.
joi.string is the most popular function in joi (40578 examples)