Registry / testing / common-schema

common-schema

JSON →
library4.7.3jsnpmunverified

Schema handling utilities for validating, processing, and normalizing objects against defined schemas. Version 4.7.3 provides a framework with support for strict and shorthand schema definitions (including mongoose-style constructors), validation via schema.validate() and normalization via schema.normalize(), with type coercion. It is actively maintained, ships TypeScript definitions, and requires Node >= 16. It offers flexible schema definitions, both strict and shorthand formats, and detailed ValidationError objects with field-level error codes. Key differentiators include deep normalization with type coercion and a rich error reporting structure.

npm install common-schema
INSTALL
IMPORT
SIG · COMMON-SCHEMA
C
common-schema
testingjavascriptv4.7.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.

createSchema
import { createSchema } from 'common-schema'
const createSchema = require('common-schema').createSchema
ESM is supported; the package exports createSchema as a named export. CommonJS require works but is not recommended.
ValidationError
import { ValidationError } from 'common-schema'
const ValidationError = require('common-schema').ValidationError
ValidationError is a class available as a named export.
default
import commonSchema from 'common-schema'
const commonSchema = require('common-schema').default
The package exports a default object containing createSchema, ValidationError, and other utilities. Use named imports for tree-shaking.

Creates a schema with nested objects, arrays, and required fields, then validates and normalizes data with type coercion.

import { createSchema } from 'common-schema'; const schema = createSchema({ foo: String, bar: { baz: [Number], qux: { type: Date, required: true } } }); // Validation console.log(schema.validate({ foo: 'test', bar: { baz: [1,2,3], qux: new Date() } })); // true // Normalization with coercion const normalized = schema.normalize({ foo: 5, bar: { baz: ['1', 2], qux: '2024-01-01T00:00:00Z' } }); console.log(normalized); // { foo: '5', bar: { baz: [1, 2], qux: 2024-01-01T00:00:00.000Z } }
Debug
Known issues
breakingIn v4, the built-in type 'buffer' has been removed; use 'blob' or custom type instead.
fix
Replace 'buffer' with 'blob' in your schema definitions.
affects: >=4.0.0
breakingIn v4, the validation error structure changed: fieldErrors is nested under data.fieldErrors instead of directly on error.
fix
Access error.data.fieldErrors instead of error.fieldErrors.
affects: >=4.0.0
deprecatedThe shorthand `{ type: 'number' }` style is deprecated in favor of constructor `Number` or string shorthand 'number'.
fix
Use `Number` or `'number'` instead of `{ type: 'number' }`.
affects: >=4.0.0
gotchaNormalization modifies the input object in-place; to avoid mutation, deep clone before passing.
fix
Use a clone: `const result = schema.normalize(JSON.parse(JSON.stringify(input)))`.
affects: >=0.0.0
gotchaThe `isValid` method returns a boolean but still runs normalization internally; performance may degrade for large objects.
fix
Use `schema.validate()` inside a try-catch if you need error details.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: common_schema_1.default.createSchema is not a function
Importing the default export and calling .createSchema as a method.
fix
Use named import: `import { createSchema } from 'common-schema'`
Error: Cannot find module 'xerror'
Missing peer dependency 'xerror'.
fix
Install xerror: `npm install xerror`
ValidationError: Invalid type: buffer
Using the removed 'buffer' type in schema.
fix
Replace 'buffer' with 'blob' or a custom validator.
Upgrade
Version history
4.7.3latest on npm
Audit
Dependencies
xerrorrequiredValidationError inherits from XError
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
common-schema — npm install common-schema · libregistry