Changed schema passing method to strings instead of constants;

Added schema name resolution function in schema.js and schemas list
This commit is contained in:
alexandercerutti
2018-08-28 02:04:06 +02:00
parent 5e2dd8a674
commit d7c003149a
3 changed files with 33 additions and 24 deletions

View File

@@ -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);

View File

@@ -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 || "";

View File

@@ -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
};