@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
52 lines • 2.18 kB
JavaScript
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { expectedType } from '@backland/utils';
import { FieldType } from './FieldType';
export class IntField extends FieldType {
constructor() {
var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
super({
def: def,
name: 'int'
});
_defineProperty(this, "parse", void 0);
var {
min,
max
} = def;
expectedType({
max,
min
}, 'number', true);
this.parse = this.applyParser({
parse: input => {
expectedType({
value: input
}, 'number');
if (!Number.isInteger(input)) {
throw new Error("".concat(input, " is not a valid integer."));
}
if (max !== undefined && input > max) {
throw new Error("".concat(input, " is more than the maximum ").concat(max, "."));
}
if (min !== undefined && input < min) {
throw new Error("".concat(input, " is less than the minimum ").concat(min, "."));
}
return input;
},
preParse(input) {
if (typeof input === 'string' && input !== '') {
var asNumber = +input;
if (!isNaN(asNumber)) return asNumber;
}
return input;
}
});
}
}
_defineProperty(IntField, "create", function () {
var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new IntField(def);
});
//# sourceMappingURL=IntField.js.map