|
| 1 | +'use strict' |
| 2 | +const { BaseSchema } = require('./BaseSchema') |
| 3 | +const { NullSchema } = require('./NullSchema') |
| 4 | +const { BooleanSchema } = require('./BooleanSchema') |
| 5 | +const { StringSchema } = require('./StringSchema') |
| 6 | +const { NumberSchema } = require('./NumberSchema') |
| 7 | +const { IntegerSchema } = require('./IntegerSchema') |
| 8 | +const { ObjectSchema } = require('./ObjectSchema') |
| 9 | +const { ArraySchema } = require('./ArraySchema') |
| 10 | + |
| 11 | +const { TYPES, setAttribute } = require('./utils') |
| 12 | + |
| 13 | +const initialState = { |
| 14 | + type: [], |
| 15 | + definitions: [], |
| 16 | + properties: [], |
| 17 | + required: [], |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents a MixedSchema. |
| 22 | + * @param {Object} [options] - Options |
| 23 | + * @param {MixedSchema} [options.schema] - Default schema |
| 24 | + * @param {boolean} [options.generateIds = false] - generate the id automatically e.g. #properties.foo |
| 25 | + * @returns {StringSchema} |
| 26 | + */ |
| 27 | + |
| 28 | +const MixedSchema = ({ schema = initialState, ...options } = {}) => { |
| 29 | + options = { |
| 30 | + generateIds: false, |
| 31 | + factory: MixedSchema, |
| 32 | + ...options, |
| 33 | + } |
| 34 | + return { |
| 35 | + ...(schema.type.includes(TYPES.STRING) |
| 36 | + ? StringSchema({ ...options, schema, factory: MixedSchema }) |
| 37 | + : {}), |
| 38 | + ...(schema.type.includes(TYPES.NUMBER) |
| 39 | + ? NumberSchema({ ...options, schema, factory: MixedSchema }) |
| 40 | + : {}), |
| 41 | + ...(schema.type.includes(TYPES.BOOLEAN) |
| 42 | + ? BooleanSchema({ ...options, schema, factory: MixedSchema }) |
| 43 | + : {}), |
| 44 | + ...(schema.type.includes(TYPES.INTEGER) |
| 45 | + ? IntegerSchema({ ...options, schema, factory: MixedSchema }) |
| 46 | + : {}), |
| 47 | + ...(schema.type.includes(TYPES.OBJECT) |
| 48 | + ? ObjectSchema({ ...options, schema, factory: MixedSchema }) |
| 49 | + : {}), |
| 50 | + ...(schema.type.includes(TYPES.ARRAY) |
| 51 | + ? ArraySchema({ ...options, schema, factory: MixedSchema }) |
| 52 | + : {}), |
| 53 | + ...(schema.type.includes(TYPES.NULL) |
| 54 | + ? NullSchema({ ...options, schema, factory: MixedSchema }) |
| 55 | + : {}), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +module.exports = { |
| 60 | + MixedSchema, |
| 61 | + default: MixedSchema, |
| 62 | +} |
0 commit comments