@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
47 lines (46 loc) • 1.54 kB
JavaScript
import SpruceError from '../errors/SpruceError.js';
import AbstractField from './AbstractField.js';
class NumberField extends AbstractField {
static generateTemplateDetails(options) {
return {
valueType: `number${options.definition.isArray ? '[]' : ''}`,
};
}
toValueType(value) {
const numberValue = +value;
if (!this.isNumber(numberValue)) {
throw new SpruceError({
code: 'TRANSFORMATION_ERROR',
fieldType: 'number',
incomingTypeof: typeof value,
incomingValue: value,
errors: [
this.buildNaNError(`${JSON.stringify(value)} could not be converted to a number.`),
],
name: this.name,
});
}
return numberValue;
}
buildNaNError(msg) {
return {
friendlyMessage: msg,
code: 'INVALID_PARAMETER',
name: this.name,
};
}
validate(value, options) {
const errors = super.validate(value, options);
if (errors.length === 0) {
if (!this.isNumber(value)) {
errors.push(this.buildNaNError(`"${JSON.stringify(value)}" is not a number!`));
}
}
return errors;
}
isNumber(value) {
return typeof value === 'undefined' || value === null || !isNaN(value);
}
}
NumberField.description = 'Handles all types of numbers with min/max and clamp support';
export default NumberField;