Package Exports
- nox-mongo-driver
- nox-mongo-driver/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nox-mongo-driver) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
๐ก๏ธ NOX Async Schema Sanitizer
A powerful, async-first, schema-based payload sanitizer and validator for Node.js & MongoDB applications.
Supports:
- โ Strict / Transform / Validate modes
- โ Type coercion
- โ Rule validation
- โ Nested objects & arrays
- โ
Explicit
Object/Arrayschemas - โ Recursive schemas (lazy functions)
- โ Custom sync & async validators
- โ Partial update mode
- โ Unknown field handling
- โ Cleanup options (remove null/empty)
- โ Bulk sanitization
- โ Prototype pollution protection
๐ฆ Installation
npm install nox-mongo-driver๐ Basic Usage
import { sanitize } from "nox-mongo-driver";
const schema = {
name: { type: "String", required: true, trim: true },
age: { type: "Number", min: 18 },
};
const payload = {
name: " Sahil ",
age: "25",
};
const result = await sanitize(payload, schema, {
mode: "strict",
});
console.log(result.value);
console.log(result.errors);โ๏ธ Modes
| Mode | Type Validation | Rule Validation | Transformation |
|---|---|---|---|
strict |
โ | โ | โ |
transform |
โ | โ | โ |
validate |
โ | โ | โ |
Example
await sanitize(payload, schema, {
mode: "transform",
});๐ง Supported Field Options
{
type: "String" | "Number" | "Boolean" | "Date" | "ObjectId" | "Object" | "Array" | "Mixed",
required: boolean,
default: any,
enum: [],
match: RegExp,
min: number,
max: number,
trim: boolean,
lowercase: boolean,
uppercase: boolean,
validate: (value, context) => boolean | string,
asyncValidate: async (value, context) => boolean | string
}๐ Type Coercion
When in strict or transform mode:
{
age: { type: "Number" },
isActive: { type: "Boolean" },
createdAt: { type: "Date" }
}Input:
{ age: "25", isActive: "true" }Output:
{ age: 25, isActive: true }๐งฉ Nested Objects (Inline)
const schema = {
profile: {
age: { type: "Number", required: true },
email: { type: "String", match: /^[^@]+@[^@]+$/ }
}
};๐งฑ Explicit Object Schema
const schema = {
profile: {
type: "Object",
required: true,
schema: {
age: { type: "Number", required: true }
}
}
};๐ Arrays
Array of Primitives
tags: { type: ["String"] }Inline Array of Objects
items: [
{
name: { type: "String", required: true }
}
]Explicit Array Schema
items: {
type: "Array",
required: true,
schema: {
name: { type: "String", required: true },
value: { type: "Number", min: 0 }
}
}โป๏ธ Recursive Schemas (Lazy)
Supports self-referencing schemas using functions:
const MenuItemSchema = () => ({
id: { type: "String", required: true },
children: [MenuItemSchema]
});
const schema = {
modules: [MenuItemSchema]
};Also works with explicit style:
children: {
type: "Array",
schema: MenuItemSchema
}๐งช Custom Validation
Sync Validation
username: {
type: "String",
validate: (value) => value.length >= 3 || "Too short"
}Async Validation
email: {
type: "String",
asyncValidate: async (value, context) => {
const exists = await context.db.findUser(value);
return !exists || "Email already exists";
}
}Pass context:
await sanitize(payload, schema, {
context: { db }
});๐ Unknown Field Handling
unknownFields: "strip" // default
unknownFields: "keep"
unknownFields: "error"โ๏ธ Cleanup Options
{
removeNull: true,
removeUndefined: true,
removeEmptyObjects: true,
removeEmptyArrays: true
}๐งฉ Partial Mode (For Updates)
await sanitize(updatePayload, schema, {
partial: true
});- Skips required validation for missing fields
- Ideal for PATCH / update APIs
๐ Stop On First Error
stopOnFirstError: trueReturns immediately on first validation failure.
๐ฆ Bulk Sanitization
import { sanitizeBulk } from "nox-mongo-driver";
const result = await sanitizeBulk(payloads, schema);
console.log(result.valid);
console.log(result.invalid);Returns:
{
valid: [],
invalid: [
{
index: number,
errors: [],
partial: {}
}
]
}๐งฏ Security Features
- Prevents
__proto__ - Prevents
constructor - Prevents
prototype - Protects against prototype pollution
๐ Error Format
{
path: "profile.age",
code: "MIN_VIOLATION",
message: "Minimum allowed is 18",
meta: { min: 18, received: 15 }
}๐ง Error Codes
- FIELD_REQUIRED
- INVALID_TYPE
- INVALID_NUMBER
- INVALID_BOOLEAN
- INVALID_DATE
- INVALID_OBJECT_ID
- ENUM_MISMATCH
- REGEX_MISMATCH
- MIN_VIOLATION
- MAX_VIOLATION
- EXPECTED_ARRAY
- EXPECTED_OBJECT
- UNKNOWN_FIELD
- CUSTOM_VALIDATION
- CUSTOM_ASYNC_VALIDATION
๐งน Cleaning + Validation Combined Example
await sanitize(payload, schema, {
mode: "strict",
removeNull: true,
removeEmptyObjects: true
});๐๏ธ Designed For
- REST APIs
- MongoDB projects
- Multi-tenant systems
- Schema-driven validation
- Secure update handling
- Deep recursive structures
๐ Recommended Usage Pattern
For Create:
mode: "strict",
partial: falseFor Update:
mode: "strict",
partial: trueFor Data Transformation Layer:
mode: "transform"๐ What This Library Is
- Not tied to Mongoose
- Works with native MongoDB driver
- Fully async
- Schema-driven
- Lightweight
- Production-ready