diff --git a/fields.js b/fields.js index 9e0af38..c75ccd4 100644 --- a/fields.js +++ b/fields.js @@ -25,7 +25,7 @@ class FieldsContainer { fields = fields[0]; } - let validFields = fields.filter(f => typeof f === "object" && schema.isValid(f, schema.constants.field)); + let validFields = fields.filter(f => typeof f === "object" && schema.isValid(f, "field")); this.fields.push(...validFields); diff --git a/index.js b/index.js index 5f7a043..99419b3 100644 --- a/index.js +++ b/index.js @@ -239,7 +239,7 @@ class Pass { data = [data]; } - let valid = data.filter(d => schema.isValid(d, schema.constants[type+"Dict"])); + let valid = data.filter(d => schema.isValid(d, type+"Dict")); this.props[type] = valid; return Object.assign({ @@ -302,10 +302,7 @@ class Pass { return false; } - // messageEncoding is required - // b.messageEncoding = b.messageEncoding || "iso-8859-1"; - - return schema.isValid(b, schema.constants.barcode); + return schema.isValid(b, "barcode"); }); this.props["barcode"] = valid[0] || {}; @@ -406,7 +403,7 @@ class Pass { data = data[0]; } - let valid = data.filter(d => d instanceof Object && schema.isValid(d, schema.constants.nfcDict)); + let valid = data.filter(d => d instanceof Object && schema.isValid(d, "nfcDict")); this.props["nfc"] = valid; @@ -435,7 +432,7 @@ class Pass { let type = passTypes[index]; this.type = type; - return schema.isValid(passFile[type], schema.constants.passDict); + return schema.isValid(passFile[type], "passDict"); } catch (e) { return false; } @@ -564,7 +561,7 @@ class Pass { */ _parseSettings(options) { - if (!schema.isValid(options, schema.constants.instance)) { + if (!schema.isValid(options, "instance")) { throw new Error(errors.REQS_NOT_MET); } @@ -574,7 +571,7 @@ class Pass { this.model = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".pass" : ""); - const filteredOpts = schema.filter(options, schema.constants.supportedOptions); + const filteredOpts = schema.filter(options.overrides, "supportedOptions"); Object.assign(this.props, filteredOpts); @@ -606,7 +603,7 @@ class Pass { } set transitType(v) { - if (schema.isValid(v, schema.constants.transitType)) { + if (schema.isValid(v, "transitType")) { this._transitType = v; } else { this._transitType = this._transitType || ""; diff --git a/schema.js b/schema.js index 7c32f99..7eb05c5 100644 --- a/schema.js +++ b/schema.js @@ -2,7 +2,7 @@ const Joi = require("joi"); const debug = require("debug")("Schema"); let instance = Joi.object().keys({ - model: Joi.string(), + model: Joi.string().required(), certificates: Joi.object().keys({ wwdr: Joi.string().required(), signerCert: Joi.string().required(), @@ -94,8 +94,31 @@ let nfcDict = Joi.object().keys({ encryptionPublicKey: Joi.string() }); +let schemas = { + instance, + barcode, + field, + passDict, + beaconsDict, + locationsDict, + transitType, + nfcDict, + supportedOptions +}; + +let resolveSchemaName = (name) => { + return schemas[name] || ""; +}; + let isValid = (opts, schemaName) => { - let validation = Joi.validate(opts, schemaName); + let resolvedSchema = resolveSchemaName(schemaName); + + if (!resolvedSchema) { + debug(`validation failed due to missing or mispelled schema name`); + return false; + } + + let validation = Joi.validate(opts, resolvedSchema); if (validation.error) { debug(`validation failed due to error: ${validation.error.message}`); @@ -121,17 +144,6 @@ let filter = (opts, schemaName) => { } module.exports = { - constants: { - instance, - barcode, - field, - passDict, - beaconsDict, - locationsDict, - transitType, - nfcDict, - supportedOptions - }, isValid, filter };