UNPKG

ottoman

Version:
90 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.numberTypeFactory = exports.NumberType = void 0; const core_type_1 = require("./core-type"); const helpers_1 = require("../helpers"); const errors_1 = require("../errors"); const utils_1 = require("../../utils"); const cast_strategy_1 = require("../../utils/cast-strategy"); const type_helpers_1 = require("../../utils/type-helpers"); /** * `Number` are plain JavaScript Number. * * ## Options * * - **required** flag to define if the field is mandatory * - **validator** that will be applied to the field a validation function, validation object or string with the name of the custom validator * - **default** that will define the initial value of the field, this option allows a value or a function * - **immutable** that will define this field as immutable. Ottoman prevents you from changing immutable fields if the schema as configure like strict * - **intVal** flag that will allow only integer values * - **min** minimum numerical value value that will be accepted * - **max** maximum numeric value that will be accepted * * @example * ```typescript * const userSchema = new Schema({ * age: Number, * }) * ``` */ class NumberType extends core_type_1.CoreType { constructor(name, options) { super(name, NumberType.sName, options); } get max() { const _options = this.options; return _options.max; } get min() { const _options = this.options; return _options.min; } get intVal() { const _options = this.options; return typeof _options.intVal === 'undefined' ? false : _options.intVal; } cast(value, strategy = cast_strategy_1.CAST_STRATEGY.DEFAULT_OR_DROP) { const castedValue = Number(value); if ((0, type_helpers_1.isNumber)(castedValue)) { return castedValue; } else { return (0, cast_strategy_1.checkCastStrategy)(value, strategy, this); } } validate(value, strategy) { value = super.validate(value, strategy); if (this.isEmpty(value)) return value; const _value = Number(value); let errors = []; const _wrongType = this.isStrictStrategy(strategy) ? !(0, utils_1.is)(value, Number) : isNaN(_value); if (_wrongType) { throw new errors_1.ValidationError(`Property '${this.name}' must be of type '${this.typeName}'`); } if (this.intVal && _value % 1 !== 0) { errors.push(`Property ${this.name} only allows Integer values`); } this.checkValidator(_value); errors.push(this._checkMin(_value)); errors.push(this._checkMax(_value)); errors = errors.filter((e) => e !== ''); if (errors.length > 0) { throw new errors_1.ValidationError(errors.join('\n')); } return _value; } _checkMin(val) { const _min = typeof this.min === 'function' ? this.min() : this.min; return (0, helpers_1.validateMinLimit)(val, _min, this.name) || ''; } _checkMax(val) { const _max = typeof this.max === 'function' ? this.max() : this.max; return (0, helpers_1.validateMaxLimit)(val, _max, this.name) || ''; } } exports.NumberType = NumberType; NumberType.sName = Number.name; const numberTypeFactory = (name, otps) => new NumberType(name, otps); exports.numberTypeFactory = numberTypeFactory; //# sourceMappingURL=number-type.js.map