Changed validation method in barcode; added function 'getValidated' to obtain validation content

This commit is contained in:
alexandercerutti
2018-09-11 19:28:13 +02:00
parent b3e8ea1e4a
commit 4a790d4161
2 changed files with 28 additions and 7 deletions

View File

@@ -317,12 +317,21 @@ class Pass {
data = [data]; 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; let valid = data
this.props["barcodes"] = valid || []; .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({ return Object.assign({
length: valid.length, length: valid.length,

View File

@@ -32,7 +32,7 @@ let supportedOptions = Joi.object().keys({
let barcode = Joi.object().keys({ let barcode = Joi.object().keys({
altText: Joi.string(), 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"), format: Joi.string().required().regex(/(PKBarcodeFormatQR|PKBarcodeFormatPDF417|PKBarcodeFormatAztec|PKBarcodeFormatCode128)/, "barcodeType"),
message: Joi.string().required() message: Joi.string().required()
}); });
@@ -140,9 +140,21 @@ let filter = (opts, schemaName) => {
return acc; return acc;
}, isObject ? {} : []); }, 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 = { module.exports = {
isValid, isValid,
filter filter,
getValidated
}; };