Registry / database / mongoose-array-validator

mongoose-array-validator

JSON →
library1.0.3jsnpmunverified

Mongoose schema plugin that adds array validation constraints such as minItems, maxItems, and uniqueItems to your schemas. Version 1.0.3 is the latest and only stable release; no active development seen. Unlike manual validation in middleware, this plugin integrates directly into schema path options. Requires Mongoose as a peer dependency. Note that the plugin must be applied via schema.plugin().

npm install mongoose-array-validator
INSTALL
IMPORT
SIG · MONGOOSE-ARRAY-VAL
M
mongoose-array-validator
databasejavascriptv1.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

arrayValidator
const arrayValidator = require('mongoose-array-validator');
import arrayValidator from 'mongoose-array-validator';
Package does not export ESM; use CommonJS require(). Default export is the plugin function.
plugin usage
const schema = new mongoose.Schema({ arr: { type: [String], minItems: 3 } }); schema.plugin(arrayValidator);
const schema = new mongoose.Schema({ arr: { type: [String], minItems: 3, plugin: arrayValidator } });
The plugin must be applied via schema.plugin(), not as a schema option.
options with message callback
minItems: { value: 2, message: props => `...` }
minItems: { value: 2, msg: props => `...` }
Use 'message' property for custom error messages, not 'msg'.

Creates a Mongoose schema with array validations (minItems, maxItems, uniqueItems) using the plugin and validates a document with duplicate values.

const mongoose = require('mongoose'); const arrayValidator = require('mongoose-array-validator'); const schema = new mongoose.Schema({ tags: { type: [String], minItems: 1, maxItems: 5, uniqueItems: true } }); schema.plugin(arrayValidator); const Model = mongoose.model('Test', schema); const doc = new Model({ tags: ['a', 'b', 'a'] }); doc.validate(err => { if (err) console.log(err.errors.tags.message); // 'No duplicates allowed!' });
Debug
Known issues
breakingRequires Mongoose peer dependency; incompatible with versions <4.x or >=7.x
fix
Ensure Mongoose version is 4.x, 5.x, or 6.x installed.
affects: >=1.0 <=1.0.3
deprecatedNo updates since 2018; no ESM support, no TypeScript types
fix
Consider migrating to Mongoose built-in array validators or custom middleware.
affects: >=1.0.0
gotchaThe plugin must be applied after schema definition; order matters with other plugins
fix
Always call schema.plugin(arrayValidator) before compiling the model.
affects: *
gotchaCustom message property must be named 'message', not 'msg' or 'errorMessage'
fix
Use { value: ..., message: props => `...` } exactly.
affects: *
Errors
Common errors & fixes
TypeError: arrayValidator is not a function
Using ES6 import syntax on a CommonJS module
fix
Change import to const arrayValidator = require('mongoose-array-validator');
ValidationError: Path `myArray` is not of type Array.
Schema path defined without array type or plugin not applied
fix
Ensure the path type is an array (e.g., [String]) and schema.plugin(arrayValidator) is called.
Cannot read property 'message' of undefined
Using message callback with incorrect property name (e.g., msg instead of message)
fix
Use property named 'message' in the options object.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
mongooserequiredpeer dependency; the plugin extends Mongoose schema functionality
Agent activity
9 hits · last 30 days
node
8
Resources
mongoose-array-validator — npm install mongoose-array-validator · libregistry