Created
November 16, 2018 13:21
-
-
Save sagar-gavhane/29e3ccc2e6526b4eae132326206312d9 to your computer and use it in GitHub Desktop.
Learning Joi
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
| const Joi = require("joi"); | |
| const nanoid = require("nanoid"); | |
| const value = { | |
| documents: [ | |
| { | |
| type: "PAN_CARD", | |
| number: nanoid(10), | |
| expire_date: "2018-11-16T18:46:19-0700", | |
| proof_for: "PROOF_OF_IDENTITY", | |
| files: [ | |
| { | |
| name: nanoid(), | |
| page: "1", | |
| type: "image/jpeg", | |
| size: "612793", | |
| width: "300", | |
| height: "500" | |
| } | |
| ] | |
| } | |
| ] | |
| }; | |
| const documentTypesEnum = [ | |
| "PAN_CARD", | |
| "AADHAAR_CARD", | |
| "DRIVING_LICENSE", | |
| "PASSPORT", | |
| "VOTOR_ID" | |
| ]; | |
| const proofForEnum = [ | |
| "PROOF_OF_IDENTITY", | |
| "PROOF_OF_ADDRESS", | |
| "PROOF_OF_CANCELLED_CHEQUE", | |
| "PROOF_OF_SIGNATURE", | |
| "PROOF_OF_SELF_RECORDED_VIDEO" | |
| ]; | |
| const fileTypeEnum = [ | |
| "image/jpeg", | |
| "image/png", | |
| "video/mp4", | |
| "video/ogg", | |
| "video/webm" | |
| ]; | |
| const filesSchema = Joi.object().keys({ | |
| name: Joi.string() | |
| .length(21) | |
| .required(), | |
| type: Joi.string() | |
| .valid(fileTypeEnum) | |
| .required(), | |
| size: Joi.string().required(), | |
| width: Joi.string().required(), | |
| height: Joi.string().required() | |
| }); | |
| const itemsSchema = Joi.object().keys({ | |
| type: Joi.string() | |
| .valid(documentTypesEnum) | |
| .required(), | |
| number: Joi.string() | |
| .when("type", { | |
| is: "PASSPORT", | |
| then: Joi.string() | |
| .length(7) | |
| .required() | |
| }) | |
| .when("type", { | |
| is: "PAN_CARD", | |
| then: Joi.string() | |
| .length(10) | |
| .required() | |
| }) | |
| .when("type", { | |
| is: "VOTOR_ID", | |
| then: Joi.string() | |
| .length(11) | |
| .required() | |
| }) | |
| .when("type", { | |
| is: "AADHAAR_CARD", | |
| then: Joi.string() | |
| .length(12) | |
| .required() | |
| }) | |
| .when("type", { | |
| is: "DRIVING_LICENSE", | |
| then: Joi.string() | |
| .length(13) | |
| .required() | |
| }) | |
| .required(), | |
| expire_date: Joi.string() | |
| .isoDate() | |
| .required(), | |
| proof_for: Joi.string() | |
| .valid(proofForEnum) | |
| .optional(), | |
| files: Joi.array() | |
| .items(filesSchema) | |
| .when("type", { | |
| is: ["AADHAAR_CARD", "VOTOR_ID", "PASSPORT"], | |
| then: Joi.array().min(2) | |
| }) | |
| .required() | |
| }); | |
| const documentSchema = Joi.object().keys({ | |
| documents: Joi.array() | |
| .items(itemsSchema) | |
| .required() | |
| }); | |
| const options = { | |
| abortEarly: false, | |
| convert: false, | |
| escapeHtml: true, | |
| stripUnknown: true | |
| }; | |
| Joi.validate(value, documentSchema, options, (err, value) => { | |
| if (err) { | |
| console.log("err", err); | |
| } else { | |
| console.log("document is valid"); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment