SchemaBoolean


SchemaBoolean()

Parameters:
  • path «String»
  • options «Object»
Inherits:

Boolean SchemaType constructor.


SchemaBoolean.checkRequired()

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

Override the function the required validator uses to check whether a boolean passes the required check.


SchemaBoolean.convertToFalse

Type:
  • «Set»

Configure which values get casted to false.

Example:

const M = mongoose.model('Test', new Schema({ b: Boolean }));
new M({ b: 'nay' }).b; // undefined
mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
new M({ b: 'nay' }).b; // false

SchemaBoolean.convertToTrue

Type:
  • «Set»

Configure which values get casted to true.

Example:

const M = mongoose.model('Test', new Schema({ b: Boolean }));
new M({ b: 'affirmative' }).b; // undefined
mongoose.Schema.Boolean.convertToTrue.add('affirmative');
new M({ b: 'affirmative' }).b; // true

SchemaBoolean.get()

Parameters:
  • getter «Function»
Returns:
  • «this»
Type:
  • «property»

Attaches a getter for all Boolean instances

Example:

mongoose.Schema.Boolean.get(v => v === true ? 'yes' : 'no');

const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
new Order({ isPaid: false }).isPaid; // 'no'

SchemaBoolean.get()

Parameters:
  • caster «Function»
Returns:
  • «Function»
Type:
  • «property»

Get/set the function used to cast arbitrary values to booleans.

Example:

// Make Mongoose cast empty string '' to false.
const original = mongoose.Schema.Boolean.cast();
mongoose.Schema.Boolean.cast(v => {
  if (v === '') {
    return false;
  }
  return original(v);
});

// Or disable casting entirely
mongoose.Schema.Boolean.cast(false);

SchemaBoolean.prototype.checkRequired()

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

Check if the given value satisfies a required validator. For a boolean to satisfy a required validator, it must be strictly equal to true or to false.


SchemaBoolean.schemaName

Type:
  • «property»

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


SchemaBoolean.set()

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

  • value «Any» value for option

Returns:
  • «undefined,void»
Type:
  • «property»

Sets a default option for all Boolean instances.

Example:

// Make all booleans have `default` of false.
mongoose.Schema.Boolean.set('default', false);

const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
new Order({ }).isPaid; // false