Registry / devops / json-rest-schema

json-rest-schema

JSON →
library1.0.16jsnpmunverified

A flexible schema validation library for JavaScript objects, designed for REST APIs and beyond. Version 1.0.16 supports Node >=18.0.0. Key features include type casting (e.g., string to number), data transformation (trimming whitespace), and a pluggable architecture for custom rules. Unlike many validators, it offers create/replace/patch semantics, nested object and array validation, recursive runtime validation, and adapters for React Hook Form, Vue+Vuetify, and VeeValidate. The library returns a flat error map with dotted paths for nested fields, making it easy to associate errors with UI components.

npm install json-rest-schema
INSTALL
IMPORT
SIG · JSON-REST-SCHEMA
J
json-rest-schema
devopsjavascriptv1.0.16
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.

createSchema
import { createSchema } from 'json-rest-schema'
const createSchema = require('json-rest-schema')
ESM-only module; CJS require() will fail with ERR_REQUIRE_ESM.
Schema
import type { Schema } from 'json-rest-schema'
import { Schema } from 'json-rest-schema'
Type import only for TypeScript; not a runtime export.
ValidationResult
import type { ValidationResult } from 'json-rest-schema'
Type for the return value { validatedObject, errors }.

Create a schema, validate user input with type casting and transformation, and handle errors.

import { createSchema } from 'json-rest-schema'; const userSchema = createSchema({ username: { type: 'string', required: true, minLength: 3 }, email: { type: 'string', required: true }, age: { type: 'number', min: 18, defaultTo: 18 } }); const userInput = { username: ' alex ', email: 'alex@example.com', age: '25' }; const { validatedObject, errors } = userSchema.create(userInput); if (Object.keys(errors).length > 0) { console.log('Validation failed!', errors); } else { console.log('Validation successful!', validatedObject); } // validatedObject: { username: 'alex', email: 'alex@example.com', age: 25 }
Debug
Known issues
breakingESM-only module: require() will throw ERR_REQUIRE_ESM.
fix
Use import syntax (ESM) or dynamic import().
affects: >=1.0.0
breakingNode.js >=18.0.0 required; older versions will fail with SyntaxError for top-level await or ESM syntax.
fix
Upgrade Node.js to version 18 or later.
affects: >=1.0.0
breakingThe 'errors' object is a plain object (not a Map) despite documentation describing it as 'a map'. Do not treat it as a Map.
fix
Use Object.keys(errors) or for...in to iterate errors, not .get/.set methods.
affects: >=1.0.0
breakingField names in errors use dot notation for nested paths (e.g., 'address.city'). Accessing via path string is correct but may be unintuitive for deeply nested arrays (e.g., 'items.0.name').
fix
Always use the full dot-separated path as the key in errors.
affects: >=1.0.0
gotchaType casting only applies to primitive types (string, number, boolean); it does not cast objects or arrays.
fix
Manually cast non-primitive types in custom rules or preprocess data.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Using CommonJS require() with an ESM-only package.
fix
Change to import statement or use dynamic import().
SyntaxError: Unexpected token 'export'
Running the module in a Node.js version <18 that doesn't support ESM syntax.
fix
Upgrade Node.js to >=18.0.0.
TypeError: Cannot read properties of undefined (reading 'field')
Attempting to access a property on an error value that may be undefined or malformed.
fix
Check that the error object exists and has the expected structure before accessing properties like .field.
Error: unknown type handler for 'customType'
Using a type in the schema that has not been registered via the plugin system.
fix
Register a custom type handler using the plugin API before using it in a schema.
Upgrade
Version history
1.0.16latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
json-rest-schema — npm install json-rest-schema · libregistry