UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

53 lines (52 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntField = void 0; var _utils = require("@backland/utils"); var _FieldType = require("./FieldType"); class IntField extends _FieldType.FieldType { constructor(def = {}) { super({ def: def, name: 'int' }); const { min, max } = def; (0, _utils.expectedType)({ max, min }, 'number', true); this.parse = this.applyParser({ parse: input => { (0, _utils.expectedType)({ value: input }, 'number'); if (!Number.isInteger(input)) { throw new Error(`${input} is not a valid integer.`); } 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 IntField(def); }; } exports.IntField = IntField; //# sourceMappingURL=IntField.js.map