Registry / testing / schema-typed

schema-typed

JSON →
library2.4.2jsnpmunverified

schema-typed is a data modeling and validation library for JavaScript and TypeScript, now at v2.4.2 (stable). It provides a fluent API to define schemas with typed validators (StringType, NumberType, ArrayType, ObjectType, BooleanType, DateType) and supports synchronous and asynchronous validation, nested object validation, field dependencies, custom rules, and schema composition via SchemaModel.combine(). Key differentiators: strong TypeScript support with full type inference for validated data, a builder pattern for readable schemas, and detailed error messages. It is maintained and widely used in the rsuite ecosystem but can be used standalone.

npm install schema-typed
INSTALL
IMPORT
SIG · SCHEMA-TYPED
S
schema-typed
testingjavascriptv2.4.2
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.

SchemaModel
import { SchemaModel } from 'schema-typed'
const SchemaModel = require('schema-typed').SchemaModel
ESM import is the standard. The package also supports CommonJS via default export, but named imports are preferred for TypeScript.
StringType
import { StringType } from 'schema-typed'
import { StringType } from 'schema-typed/StringType'
All type factories are named exports from the main entry. Do not import from subpaths.
NumberType
import { NumberType } from 'schema-typed'
import NumberType from 'schema-typed'
NumberType is a named export, not a default export. Use destructuring.

Creates a user schema with required string and number fields, then validates sample data showing error messages.

import { SchemaModel, StringType, NumberType } from 'schema-typed'; const userModel = SchemaModel({ username: StringType().isRequired('Username is required').minLength(3, 'Must be at least 3 characters'), age: NumberType().isRequired('Age is required').range(0, 150, 'Age must be between 0 and 150') }); const data = { username: 'al', age: 200 }; const checkResult = userModel.check(data); console.log(checkResult); // => { username: { hasError: false }, age: { hasError: true, errorMessage: 'Age must be between 0 and 150' } }
Debug
Known issues
gotchaThe `isRequired` method trims whitespace by default. If you need to allow empty strings, use `isRequiredOrEmpty`.
fix
Use `isRequiredOrEmpty(errorMessage)` instead of `isRequired` if you want to allow empty but still validate other rules.
affects: >=0.0.0
gotchaWhen using `equalTo`, the field name must match exactly the key in the data object. It does not support nested paths.
fix
Use a custom rule with `addRule` for nested field comparisons.
affects: >=0.0.0
gotcha`check` and `checkAsync` return an object keyed by field names, each with `hasError` and `errorMessage`. If all pass, `hasError` is `false` for each field. There is no top-level `isValid` flag.
fix
Check all fields: `Object.values(result).every(f => !f.hasError)`.
affects: >=0.0.0
gotchaThe `when` method conditionally changes the type based on other fields. But it creates a new type instance each time, which may cause performance issues if re-evaluated often.
fix
Use `when` sparingly and consider caching schemas if needed.
affects: >=2.0.0
deprecated`pattern` only exists on StringType, not on other types. For regex on numbers, convert to string first.
fix
Use `StringType().pattern(...)` on a stringified version of the number.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: SchemaModel is not a constructor
Using `new SchemaModel(...)` instead of calling it as a function.
fix
Use `const model = SchemaModel({...})` without `new`.
Cannot read properties of undefined (reading 'hasError')
Accessing `result.fieldName` where `fieldName` was not validated because it wasn't in the schema or data.
fix
Ensure the field exists in the schema and data. Use optional chaining: `result.fieldName?.hasError`.
Type 'string' is not assignable to type 'number' in NumberType().isInteger()
Data from form inputs are strings by default, but NumberType expects numeric values.
fix
Parse input to number before validation: `age: NumberType().isInteger('Must be integer').check(Number(value))`.
Upgrade
Version history
2.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
6
Amazon
1
Bingbot
1
Resources
schema-typed — npm install schema-typed · libregistry