UNPKG

ottoman

Version:
71 lines 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mixedTypeFactory = exports.MixedType = void 0; const core_type_1 = require("./core-type"); /** * `Mixed` type supports any `Object` types, you can change the value to anything else, it can be represented in the following ways: * * ## 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 * * @example * ```typescript * const schema = new Schema({ * inline: Schema.Types.Mixed, * types: { type: Schema.Types.Mixed, required: true }, * obj: Object, * empty: {}, * }); * * * schema.fields.inline instanceof MixedType; // true * schema.fields.types instanceof MixedType; // true * schema.fields.obj instanceof MixedType; // true * schema.fields.empty instanceof MixedType; // true * * const data = { * inline: { name: 'george' }, * types: { * email: 'george@gmail.com', * }, * obj: 'Hello', * empty: { hello: 'hello' }, * }; * * const result = validate(data, schema); * console.log(result); * * // Output!!! * // { * // "inline": { * // "name": "george" * // }, * // "types": { * // "email": "george@gmail.com" * // }, * // "obj": "Hello", * // "empty": { * // "hello": "hello" * // } * // } * ``` */ class MixedType extends core_type_1.CoreType { constructor(name, options) { super(name, MixedType.sName, options); } cast(value, strategy) { value = super.validate(value, strategy); this.checkValidator(value); return value; } } exports.MixedType = MixedType; MixedType.sName = 'Mixed'; const mixedTypeFactory = (name, otps) => new MixedType(name, otps); exports.mixedTypeFactory = mixedTypeFactory; //# sourceMappingURL=mixed-type.js.map