Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

schema-typed

> 编程语言
Open source

Schema for data modeling & validation

200 stars0 likes0 views
WebsiteGitHub

About

Schema for data modeling & validation

schema-typed

Schema for data modeling & validation

[![npm][npm-badge]][npm] [![GitHub Actions][actions-svg]][actions-home] [![Coverage Status][soverage-svg]][soverage]

Table of Contents

  • Installation
  • Usage
    • Getting Started
    • Multiple verification
    • Custom verification
      • Field dependency validation
      • Asynchronous check
    • Validate nested objects
    • Combine
  • API
    • SchemaModel
      • static combine(...models)
      • check(data: object)
      • checkAsync(data: object)
      • checkForField(fieldName: string, data: object, options?: { nestedObject?: boolean })
      • checkForFieldAsync(fieldName: string, data: object, options?: { nestedObject?: boolean })
    • MixedType()
      • isRequired(errorMessage?: string, trim: boolean = true)
      • isRequiredOrEmpty(errorMessage?: string, trim: boolean = true)
      • addRule(onValid: Function, errorMessage?: string, priority: boolean)
      • addAsyncRule(onValid: Function, errorMessage?: string, priority: boolean)
      • when(condition: (schemaSpec: SchemaDeclaration) => Type)
      • check(value: ValueType, data?: DataType):CheckResult
      • checkAsync(value: ValueType, data?: DataType):Promise
      • label(label: string)
      • equalTo(fieldName: string, errorMessage?: string)
      • proxy(fieldNames: string[], options?: { checkIfValueExists?: boolean })
    • StringType(errorMessage?: string)
      • isEmail(errorMessage?: string)
      • isURL(errorMessage?: string)
      • isOneOf(items: string[], errorMessage?: string)
      • containsLetter(errorMessage?: string)
      • containsUppercaseLetter(errorMessage?: string)
      • containsLowercaseLetter(errorMessage?: string)
      • containsLetterOnly(errorMessage?: string)
      • containsNumber(errorMessage?: string)
      • pattern(regExp: RegExp, errorMessage?: string)
      • rangeLength(minLength: number, maxLength: number, errorMessage?: string)
      • minLength(minLength: number, errorMessage?: string)
      • maxLength(maxLength: number, errorMessage?: string)
    • NumberType(errorMessage?: string)
      • isInteger(errorMessage?: string)
      • isOneOf(items: number[], errorMessage?: string)
      • pattern(regExp: RegExp, errorMessage?: string)
      • range(minLength: number, maxLength: number, errorMessage?: string)
      • min(min: number, errorMessage?: string)
      • max(max: number, errorMessage?: string)
    • ArrayType(errorMessage?: string)
      • isRequiredOrEmpty(errorMessage?: string)
      • rangeLength(minLength: number, maxLength: number, errorMessage?: string)
      • minLength(minLength: number, errorMessage?: string)
      • maxLength(maxLength: number, errorMessage?: string)
      • unrepeatable(errorMessage?: string)
      • of()
    • DateType(errorMessage?: string)
      • range(min: Date, max: Date, errorMessage?: string)
      • min(min: Date, errorMessage?: string)
      • max(max: Date, errorMessage?: string)
    • ObjectType(errorMessage?: string)
      • shape(fields: object)
    • BooleanType(errorMessage?: string)
  • ⚠️ Notes

Installation

bash
npm install schema-typed --save

Usage

Getting Started

…

checkResult return structure is:

javascript
{
  username: { hasError: false },
  email: { hasError: false },
  age: { hasError: true, errorMessage: 'Over the age limit' },
  tags: {
    hasError: true,
    array: [
      { hasError: false },
      { hasError: false },
      { hasError: true, errorMessage: 'The tag should be a string' }
    ]
  },
  role: {
    hasError: true,
    object: {
      name: { hasError: false },
      permissions: { hasError: true, errorMessage: 'Permissions required' }
    }
  }
};

Multiple verification

javascript
StringType()
  .minLength(6, "Can't be less than 6 characters")
  .maxLength(30, 'Cannot be greater than 30 characters')
  .isRequired('This field required');

Custom verification

Customize a rule with the addRule function.

If you are validating a string type of data, you can set a regular expression for custom validation by the pattern method.

javascript
const model = SchemaModel({
  field1: StringType().addRule((value, data) => {
    return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
  }, 'Please enter legal characters'),
  field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters')
});

model.check({ field1: '', field2: '' });

/**
{
  field1: {
    hasError: true,
    errorMessage: 'Please enter legal characters'
  },
  field2: {
    hasError: true,
    errorMessage: 'Please enter legal characters'
  }
};
**/

Field dependency validation

  1. Use the equalTo method to verify that the values of two fields are equal.
javascript
const model = SchemaModel({
  password: StringType().isRequired(),
  confirmPassword: StringType().equalTo('password')
});
  1. Use the addRule method to create a custom validation rule.
javascript
const model = SchemaModel({
  password: StringType().isRequired(),
  confirmPassword: StringType().addRule(
    (value, data) => value === data.password,
    'Confirm password must be the same as password'
  )
});
  1. Use the proxy method to verify that a field passes, and then proxy verification of other fields.
javascript
const model = SchemaModel({
  password: StringType().isRequired().proxy(['confirmPassword']),
  confirmPassword: StringType().equalTo('password')
});

Asynchronous check

For example, verify that the mailbox is duplicated

…

Validate nested objects

Validate nested objects, which can be defined using the ObjectType().shape method. E.g:

…

Combine

SchemaModel provides a static method combine that can be combined with multiple SchemaModel to return a new SchemaModel.

javascript
const model1 = SchemaModel({
  username: StringType().isRequired('This field required'),
  email: StringType().isEmail('Should be an email')
});

const model2 = SchemaModel({
  username: StringType().minLength(7, "Can't be less than 7 characters"),
  age: NumberType().range(18, 30, 'Age should be greater than 18 years old')
});

const model3 = SchemaModel({
  groupId: NumberType().isRequired('This field required')
});

const model4 = SchemaModel.combine(model1, model2, model3);

model4.check({
  username: 'foobar',
  email: '[email protected]',
  age: 40,
  groupId: 1
});

API

SchemaModel

SchemaModel is a JavaScript schema builder for data model creation and validation.

static combine(...models)

A static method for merging multiple models.

javascript
const model1 = SchemaModel({
  username: StringType().isRequired('This field required')
});

const model2 = SchemaModel({
  email: StringType().isEmail('Please input the correct email address')
});

const model3 = SchemaModel.combine(model1, model2);

check(data: object)

Check whether the data conforms to the model shape definition. Return a check result.

javascript
const model = SchemaModel({
  username: StringType().isRequired('This field required'),
  email: StringType().isEmail('Please input the correct email address')
});

model.check({
  username: 'root',
  email: '[email protected]'
});

checkAsync(data: object)

Asynchronously check whether the data conforms to the model shape definition. Return a check result.

javascript
const model = SchemaModel({
  username: StringType()
    .isRequired('This field required')
    .addRule(value => {
      return new Promise(resolve => {
        // Asynchronous processing logic
      });
    }, 'Username already exists'),
  email: StringType().isEmail('Please input the correct email address')
});

model
  .checkAsync({
    username: 'root',
    email: '[email protected]'
  })
  .then(result => {
    // Data verification result
  });

checkForField(fieldName: string, data: object, options?: { nestedObject?: boolean })

Check whether a field in the data conforms to the model shape definition. Return a check result.

javascript
const model = SchemaModel({
  username: StringType().isRequired('This field required'),
  email: StringType().isEmail('Please input the correct email address')
});

const data = {
  username: 'root'
};

model.checkForField('username', data);

checkForFieldAsync(fieldName: string, data: object, options?: { nestedObject?: boolean })

Asynchronously check whether a field in the data conforms to the model shape definition. Return a check result.

javascript
const model = SchemaModel({
  username: StringType()
    .isRequired('This field required')
    .addAsyncRule(value => {
      return new Promise(resolve => {
        // Asynchronous processing logic
      });
    }, 'Username already exists'),
  email: StringType().isEmail('Please input the correct email address')
});

const data = {
  username: 'root'
};

model.checkForFieldAsync('username', data).then(result => {
  // Data verification result
});

MixedType()

Creates a type that matches all types. All types inherit from this base type.

isRequired(errorMessage?: string, trim: boolean = true)

javascript
MixedType().isRequired('This field required');

isRequiredOrEmpty(errorMessage?: string, trim: boolean = true)

javascript
MixedType().isRequiredOrEmpty('This field required');

addRule(onValid: Function, errorMessage?: string, priority: boolean)

javascript
MixedType().addRule((value, data) => {
  return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, 'Please enter a legal character.');

addAsyncRule(onValid: Function, errorMessage?: string, priority: boolean)

javascript
MixedType().addAsyncRule((value, data) => {
  return n

Issues· 12 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptschemaschemamodelvalidator

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言