Skip to content

LSafer/mongoose-extra-validators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose Extra Validators

Extra validators to be used in mongoose

Setup

To add the plugin you need to do the following:

import mongoose from 'mongoose'
import {ExtraValidatorsPlugin} from 'mongoose-extra-validators'

mongoose.plugin(ExtraValidatorsPlugin)

The Validators

This plugin contains multiple common validators.

Each validator must be added specifically for each path.

Exists Validator

The exists validator insures an id field only stores and id that points to an existing document.

You can enable the exists validator as follows:

import {Schema, SchemaTypes} from 'mongoose'

const schema = new Schmea({
    somethingId: {
        type: SchemaTypes.ObjectId,
        /* type: string | Model | (() => string | Model) */
        ref: 'Something',
        /* type: boolean | (() => boolean) | [boolean | (() => boolean), string] */
        exists: true
    }
})

Singleton validator

The singleton validator is like the unique option but is a validator.

You can enable the singleton validator as follows:

import {Schema, SchemaTypes} from 'mongoose'

const schema = new Schmea({
    somethingUnique: {
        type: SchemaTypes.String,
        /* Although redunant, but still good to add it as a second line of defence. */
        unique: true,
        /* type: boolean | (() => boolean) | [boolean | (() => boolean), string] */
        singleton: true
    }
})

About

Mongoose Extra Validators

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages