@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
51 lines (50 loc) • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FloatField = void 0;
var _utils = require("@backland/utils");
var _FieldType = require("./FieldType");
class FloatField extends _FieldType.FieldType {
constructor(def = {}) {
super({
def: def,
name: 'float'
});
const {
min,
max
} = def;
(0, _utils.expectedType)({
max,
min
}, 'number', true);
this.parse = this.applyParser({
parse: input => {
(0, _utils.expectedType)({
value: input
}, 'number');
if (max !== undefined && input > max) {
throw new Error(`${input} is more than the maximum ${max}.`);
}
if (min !== undefined && input < min) {
throw new Error(`${input} is less than the minimum ${min}.`);
}
return input;
},
preParse(input) {
if (typeof input === 'string' && input !== '') {
const asNumber = +input;
if (!isNaN(asNumber)) return asNumber;
}
return input;
}
});
}
static create = (def = {}) => {
return new FloatField(def);
};
toString = () => `${this.typeName}(${this.def || ''})`;
}
exports.FloatField = FloatField;
//# sourceMappingURL=FloatField.js.map