SchemaArray


SchemaArray()

Parameters:
  • key «String»
  • cast «SchemaType»
  • options «Object»
  • schemaOptions «Object»
Inherits:

Array SchemaType constructor


SchemaArray.checkRequired()

Parameters:
  • fn «Function»
Returns:
  • «Function»

Override the function the required validator uses to check whether an array passes the required check.

Example:

// Require non-empty array to pass `required` check
mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);

const M = mongoose.model({ arr: { type: Array, required: true } });
new M({ arr: [] }).validateSync(); // `null`, validation fails!

SchemaArray.options

Type:
  • «property»

Options for all arrays.

  • castNonArrays: true by default. If false, Mongoose will throw a CastError when a value isn't an array. If true, Mongoose will wrap the provided value in an array before casting.

SchemaArray.prototype.checkRequired()

Parameters:
  • value «Any»
  • doc «Document»
Returns:
  • «Boolean»

Check if the given value satisfies the required validator.


SchemaArray.prototype.enum()

Parameters:
  • [...args] «String|Object» enumeration values

Returns:
  • «SchemaArray» this

Adds an enum validator if this is an array of strings or numbers. Equivalent to SchemaString.prototype.enum() or SchemaNumber.prototype.enum()


SchemaArray.schemaName

Type:
  • «property»

This schema type's name, to defend against minifiers that mangle function names.


SchemaArray.set()

Parameters:
  • option «String» The option you'd like to set the value for

  • value «Any» value for option

Returns:
  • «undefined,void»

Sets a default option for all Array instances.

Example:

// Make all Array instances have `required` of true by default.
mongoose.Schema.Array.set('required', true);

const User = mongoose.model('User', new Schema({ test: Array }));
new User({ }).validateSync().errors.test.message; // Path `test` is required.