@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
61 lines (60 loc) • 2.19 kB
JavaScript
import SpruceError from '../errors/SpruceError.js';
import AbstractField from './AbstractField.js';
class NumberField extends AbstractField {
static generateTemplateDetails(options) {
const { definition, language } = options;
const { isArray, options: fieldOptions } = definition;
const go = `${isArray ? '[]' : ''}float64`;
let validation = undefined;
if (language === 'go') {
validation = [];
if (fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.min) {
validation.push(`gte=${fieldOptions.min}`);
}
if (fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.max) {
validation.push(`lte=${fieldOptions.max}`);
}
}
return {
valueType: language === 'go' ? go : `number${isArray ? '[]' : ''}`,
validation,
};
}
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;