From 4a790d4161a44367084882cc176d9e32322008ed Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Tue, 11 Sep 2018 19:28:13 +0200 Subject: [PATCH] Changed validation method in barcode; added function 'getValidated' to obtain validation content --- index.js | 17 +++++++++++++---- schema.js | 18 +++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 90a47ab..851596c 100644 --- a/index.js +++ b/index.js @@ -317,12 +317,21 @@ class Pass { data = [data]; } - let valid = data.filter(b => b instanceof Object && schema.isValid(b, "barcode")); + // messageEncoding is required but has a default value. + // Therefore I assign a validated version of the object with the default value + // to the ones that doesn't have messageEncoding. + // if o is not a valid object, false is returned and then filtered later - this.props["barcode"] = valid[0] || undefined; - this.props["barcodes"] = valid || []; + let valid = data + .map(o => schema.getValidated(o, "barcode")) + .filter(o => o instanceof Object); - // I bind "this" to get a clean this when returning from the methods + if (valid.length) { + this.props["barcode"] = valid[0]; + this.props["barcodes"] = valid; + } + + // I bind "this" to get a clean context (without these two methods) when returning from the methods return Object.assign({ length: valid.length, diff --git a/schema.js b/schema.js index 0808d1f..0acf0e3 100644 --- a/schema.js +++ b/schema.js @@ -32,7 +32,7 @@ let supportedOptions = Joi.object().keys({ let barcode = Joi.object().keys({ altText: Joi.string(), - messageEncoding: Joi.string().default("iso-8859-1").required(), + messageEncoding: Joi.string().default("iso-8859-1"), format: Joi.string().required().regex(/(PKBarcodeFormatQR|PKBarcodeFormatPDF417|PKBarcodeFormatAztec|PKBarcodeFormatCode128)/, "barcodeType"), message: Joi.string().required() }); @@ -140,9 +140,21 @@ let filter = (opts, schemaName) => { return acc; }, isObject ? {} : []); -} +}; + +let getValidated = (opts, schemaName) => { + let resolvedSchema = resolveSchemaName(schemaName); + let validation = Joi.validate(opts, resolvedSchema); + + if (validation.error) { + return !validation.error; + } + + return validation.value; +}; module.exports = { isValid, - filter + filter, + getValidated };