JSPM

nox-mongo-driver

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q78453F
  • License MIT

A fast runtime schema-driven sanitizer & validator for dynamic schemas โ€” alternative to Mongoose validation for multi-tenant apps.

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 / Array schemas
  • โœ… 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: true

Returns 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: false

For Update:

mode: "strict",
partial: true

For 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