Created
January 7, 2020 12:33
-
-
Save arifmahmudrana/916dc3f584c896452163944bab43f00e to your computer and use it in GitHub Desktop.
JOI validation JavaScript example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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