|
| 1 | +// "input values" are what's found in the input data, and will generally be |
| 2 | +// simple string/boolean/number/date/etc things. "attribute values" are the |
| 3 | +// actual attributes in our output data model and can be some arbitrary type. |
| 4 | + |
| 5 | +// Am attribute type is a pair of functions. |
| 6 | + |
| 7 | +// One validates an input value to see if it can be made into a valid attribute |
| 8 | +// value. It should return a list of warnings and a list of errors. |
| 9 | + |
| 10 | +// One maps an input value to a value of the attribute type. |
| 11 | + |
| 12 | +// ABS TODOs: Make validation and translation happen together - drop |
| 13 | +// AttributeType and just implement it as a function from input value -> |
| 14 | +// (outputValue, [warning], [error]). |
| 15 | + |
| 16 | +// This will enable optionalType to handle errors from the base type by turning |
| 17 | +// them into warnings *and* returning undefined as the translater value. |
| 18 | + |
| 19 | +// Then add a requiredType that wraps any type and makes it "required", treating |
| 20 | +// "" or undefined inputs as errors. |
| 21 | + |
| 22 | +// Base types without being wrapped by requiredType or optionalType may then |
| 23 | +// handled undefined or "" as they see fit. |
| 24 | + |
| 25 | +class AttributeType { |
| 26 | + constructor(validator, translator) { |
| 27 | + this.v = validator; |
| 28 | + this.t = translator; |
| 29 | + } |
| 30 | + |
| 31 | + get validator() { return this.v; } |
| 32 | + get translator() { return this.t; } |
| 33 | + |
| 34 | + validate(inputValue) { return this.v(inputValue); } |
| 35 | + translate(inputValue) { return this.t(inputValue); } |
| 36 | +} |
| 37 | + |
| 38 | +exports.AttributeType = AttributeType; |
| 39 | + |
| 40 | +// Create an optional version of an existing type, that allows empty strings or |
| 41 | +// undefined values and maps then to "undefined", and converts any validation errors to warnings |
| 42 | +exports.optionalType = (baseType) => { |
| 43 | + return new AttributeType( |
| 44 | + (inputValue) => { |
| 45 | + if(inputValue !== undefined && inputValue != "") { |
| 46 | + const [warnings, errors] = baseType.validate(inputValue); |
| 47 | + // Convert any errors into warnings, as this is an optional field |
| 48 | + return [warnings.concat(errors), []]; |
| 49 | + } |
| 50 | + else { |
| 51 | + return [[],[]]; |
| 52 | + } |
| 53 | + }, |
| 54 | + (inputValue) => { |
| 55 | + if(inputValue !== undefined && inputValue != "") return baseType.translate(inputValue); |
| 56 | + else return undefined; |
| 57 | + } |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +// Create a required version of an existing type, that |
| 62 | +exports.optionalType = (baseType) => { |
| 63 | + return new AttributeType( |
| 64 | + (inputValue) => { |
| 65 | + if(inputValue !== undefined && inputValue != "") { |
| 66 | + const [warnings, errors] = baseType.validate(inputValue); |
| 67 | + // Convert any errors into warnings, as this is an optional field |
| 68 | + return [warnings.concat(errors), []]; |
| 69 | + } |
| 70 | + else { |
| 71 | + return [[],[]]; |
| 72 | + } |
| 73 | + }, |
| 74 | + (inputValue) => { |
| 75 | + if(inputValue !== undefined && inputValue != "") return baseType.translate(inputValue); |
| 76 | + else return undefined; |
| 77 | + } |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +// The default string type. Does not allow empty strings! |
| 82 | +exports.basicStringType = new AttributeType( |
| 83 | + (inputValue) => { |
| 84 | + if(inputValue === undefined || inputValue == "") { |
| 85 | + return [[],["A value must be provided"]]; |
| 86 | + } |
| 87 | + // Anything else can be converted into a string |
| 88 | + return [[],[]]; |
| 89 | + }, |
| 90 | + (inputValue) => { |
| 91 | + return String(inputValue); |
| 92 | + } |
| 93 | +); |
| 94 | + |
| 95 | +exports.basicNumberType = new AttributeType( |
| 96 | + (inputValue) => { |
| 97 | + if(inputValue === undefined || inputValue == "") { |
| 98 | + return [[],["A value must be provided"]]; |
| 99 | + } |
| 100 | + |
| 101 | + if(parseFloat(inputValue) === NaN) { |
| 102 | + return [[],[inputValue + " is not a valid number"]]; |
| 103 | + } |
| 104 | + |
| 105 | + return [[],[]]; |
| 106 | + }, |
| 107 | + (inputValue) => { |
| 108 | + return parseFloat(inputValue); |
| 109 | + } |
| 110 | +); |
0 commit comments