Registry / database / validata-mongo

validata-mongo

JSON →
library1.1.1jsnpmunverified

Type-safe data validation and sanitization for MongoDB update $set operations, built on top of the validata validation library. Current stable version is 1.1.1. It provides a set of checkers (isObjectSet, isString, maybeString, isNumber, asNumber) that validate and coerce data before applying it to MongoDB $set updates, supporting both nested object structures and dot-notation paths. Unlike generic validators, it is specifically designed to match MongoDB's update semantics, including optional fields, coercion, and rejection of unexpected properties. It ships with TypeScript definitions and has a moderate release cadence.

npm install validata-mongo
INSTALL
IMPORT
SIG · VALIDATA-MONGO
V
validata-mongo
databasejavascriptv1.1.1
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.

isObjectSet
import { isObjectSet } from 'validata-mongo';
import isObjectSet from 'validata-mongo';
isObjectSet is a named export, not a default export.
isString
import { isString } from 'validata';
import { isString } from 'validata-mongo';
isString is exported from the validata package, not validata-mongo.
asNumber
import { asNumber } from 'validata';
import { asNumber } from 'validata-mongo';
asNumber is a coercion checker from validata, not exported by validata-mongo.

Demonstrates type-safe validation of MongoDB $set data using isObjectSet with nested objects and dot-notation.

import { isObjectSet } from 'validata-mongo'; import { isString, maybeString, isNumber, asNumber, check } from 'validata'; interface Child { foo: number; bar: number; } interface MyObject { required: string; optional?: string; child: Child; } const myCheck = isObjectSet<MyObject>({ required: isString(), optional: maybeString(), child: isObjectSet({ foo: isNumber({ min: 1 }), bar: asNumber({ coerceMax: 10 }), }), }); const result = check(myCheck, () => ({ required: 'blue', child: { foo: 1, bar: 2, }, })); console.log(JSON.stringify(result)); // SUCCESS -> {"required":"blue","child":{"foo":1,"bar":2}} const result2 = check(myCheck, () => ({ required: 'blue', 'child.foo': 3, })); console.log(JSON.stringify(result2)); // SUCCESS -> {"required":"blue","child.foo":3}
Debug
Known issues
gotchaisObjectSet validates only known properties; any unexpected property causes a ValidationError with reason 'unexpected-property'.
fix
Ensure the input object does not contain properties not defined in the schema, or use a more lenient checker if needed.
affects: >=1.0.0
gotchaDot-notation paths like 'child.foo' are accepted, but the path keys must match the schema structure exactly. Using a dot notation for a property not defined in the schema results in an error.
fix
Use dot-notation only for properties that exist in the schema's nested structure.
affects: >=1.0.0
gotchaThe library expects a lazily-evaluated function (() => value) rather than a direct value. Passing the object directly causes a runtime error.
fix
wrap the data object in an arrow function: () => ({ ... })
affects: >=1.0.0
deprecatedNo deprecated features known at this version.
fix
N/A
affects: none
Errors
Common errors & fixes
TypeError: check is not a function
check is imported from 'validata' but not imported or incorrectly imported from 'validata-mongo'.
fix
import { check } from 'validata';
ValidationError: {"issues":[{"path":["wow"],"value":"blue","reason":"unexpected-property"}],"name":"ValidationError"}
The input object contains a property 'wow' that is not defined in the schema.
fix
Remove the unexpected property or add it to the schema.
Error: data must be a function
The second argument to check() must be a lazy function returning the data object, not the object itself.
fix
Change check(myCheck, { ... }) to check(myCheck, () => ({ ... }))
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
validatarequiredpeer dependency providing core validation primitives (isString, isNumber, etc.)
Agent activity
30 hits · last 30 days
node
26
Meta
1
OpenAI (training)
1
Resources
validata-mongo — npm install validata-mongo · libregistry