Skip to content

Instantly share code, notes, and snippets.

@arifmahmudrana
Created January 7, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arifmahmudrana/916dc3f584c896452163944bab43f00e to your computer and use it in GitHub Desktop.
Save arifmahmudrana/916dc3f584c896452163944bab43f00e to your computer and use it in GitHub Desktop.
JOI validation JavaScript example
import {array, object, string, validate} from "joi";
const schema = object({
searchCode: string().required().label('Search code'),
primaryContact: object({
mobileNumber: string()
.regex(/^\+?\d{7,13}$/)
.error(() => 'You must provide a valid Primary contact mobile number.')
.required()
.label('Primary contact mobile number')
}).required(),
rooms: array().required().min(1).max(6).sparse().items(
object({
id: string().required(),
guests: array().required().min(1).max(6).sparse().unique().items(
object({
givenName: string().min(1).required()
})
)
})
)
});
const validationResult = validate({
primaryContact: {
mobileNumber: '+8801234567890'
}, rooms: [{
}]
}, schema);
console.log((validationResult.error.details));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment