JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21
  • Score
    100M100P100Q42322F
  • License MIT

This is a tool for automatically validating entities by defining rules, providing boundary checking for strings, numbers, dates, enums, objects, and arrays in JavaScript/TypeScript applications.

Package Exports

  • @ticatec/web-bean-validator
  • @ticatec/web-bean-validator/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ticatec/web-bean-validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

WebBean Validation Tool

This is a tool class that automatically validates entities (boundary checking) by defining rules.

中文文档


I18N

Error messages are internationalized using @ticatec/i18n.

Resources

import {cn_resource, en_resource} from "@ticatec/web-bean-validator"
  • 英文资源
const en_resource = {
    ticatec: {
        validation: {
            required: `Please enter a value for {{field}}`,
            stringShortage: `{{field}} must be at least {{length}} characters long`,
            earliestDate: "{{field}} cannot be earlier than {{date}}",
            finalDate: "{{field}} cannot be later than {{date}}",
            numberExceed: `{{field}} cannot exceed {{max}}`,
            numberShortage: `{{field}} cannot be less than {{min}}`,
            arrayExceed: `{{field}} cannot contain more than {{length}} records`,
            arrayShortage: `{{field}} must contain at least {{length}} records`
        }
    }
}

export default en_resource;

Usage

npm i @ticatec/web-bean-validator
import beanValidator from "@ticatec/web-bean-validator";
import { BaseValidator, StringValidator, NumberValidator, DateValidator, EnumValidator, ObjectValidator, ArrayValidator } from "@ticatec/entity-validator";

let rules: Array<BaseValidator> = [
    // Define validation rules here
];

let data = {};

let result = beanValidator.validate(data, rules);

Validators

Common Validation Options

interface ValidatorOptions {
    name?: string, //field‘s name, if not assign, use field instead of name
    required?: boolean,
    check?: CustomCheck, // Custom validation function
    ignoreWhen?: IgnoreWhen  // Ignore validation when condition is met
}

String Type Validator

Constructor

interface StringValidatorOptions extends ValidatorOptions {
    name: "Name",
    minLen?: number,  // Minimum length
    format?: {
        regex: RegExp, // Regular expression
        message: string // Message for failed match
    }
}

new StringValidator(field, options);

Number Type Validator

Constructor

interface NumberValidatorOptions extends ValidatorOptions {
    minValue?: number,  // Minimum value
    maxValue?: number,  // Maximum value
}

new NumberValidator(field, options);

Date/Time Type Validator

Constructor

interface DateValidatorOptions extends ValidatorOptions {
    from?: Date, // Earliest date
    to?: Date,  // Latest date
    maxDaysBefore?: number,  // Maximum days before, 0 means starting from today
    maxDaysAfter?: number,  // Maximum days after, 0 means the latest date is today
}

new DateValidator(field, options);

Enum Type Validator

Constructor

interface EnumValidatorOptions extends ValidatorOptions {
    values: Array<any>; // Enum values
    check?: CustomCheck, // Custom validation function
}

new EnumValidator(field, options);

Boolean Type Validator

Constructor

interface BooleanValidatorOptions extends ValidatorOptions {
}

new BooleanValidator(field, options);

Object Type Validator

Constructor

interface ObjectValidatorOptions extends ValidatorOptions {
    rules: Array<BaseValidator>;
}

new ObjectValidator(field, options);

Array Type Validator

Constructor

interface ArrayValidatorOptions extends ValidatorOptions {
    rules: Array<BaseValidator>;
    minLen?: number;
    maxLen?: number;
}

new ArrayValidator(field, options);

Custom Check Methods

When special validation methods are needed that the above cannot cover, you can define a programmatic check by passing a check function.

/**
 * value: The value of the current field
 * data: The value of the current object
 */
type CustomCheck = (value: any, data: any) => string | null;

// Check that the start time must be earlier than the end time
let checkDate = (value: any, data: any) => {
    if (data.finished != null && value > data.finished) {
        return "End time cannot be earlier than start time";
    }
};

// Check for unique codes in an array
let checkDuplicatedCode = (arr: any, data: any) => {
    if (new Set(arr.map(item => item.code)).size != arr.length) {
        return "There are duplicate codes in the devices";
    }
};

I18N Support

Error messages support internationalization through @ticatec/i18n. For more information, please refer to i18n Internationalization.

Built-in Resources

import {cn_resource, en_resource} from "@ticatec/web-bean-validator"
  • English resources
const en_resource = {
    ticatec: {
        validation: {
            required: `Please enter a value`,
            stringShortage: `Length must be at least {{length}} characters`,
            earliestDate: "Time cannot be earlier than {{date}}",
            finalDate: "The latest time cannot be later than {{date}}",
            numberExceed: `Value cannot exceed {{max}}`,
            numberShortage: `Value cannot be less than {{min}}`,
            arrayExceed: `Content exceeds {{length}} records`,
            arrayShortage: `Content cannot be less than {{length}} records`
        }
    }
}

export default en_resource;
  • Chinese resources
const cn_resource = {
    ticatec: {
        validation: {
            required: `请输入数值`,
            stringShortage: `长度至少{{length}}个字符`,
            earliestDate: "时间不能早于{{date}}",
            finalDate: "最后时间不能晚于{{date}}",
            numberExceed: `数值不能超过{{max}}`,
            numberShortage: `数值不能低于{{min}}`,
            arrayExceed: `内容超过了{{length}}个记录`,
            arrayShortage: `内容不能少于{{length}}个记录`
        }
    }
}

export default cn_resource;

Example

Validate user data

import StringValidator from "./StringValidator";
import DateValidator from "./DateValidator";

let eduRules = [
    new DateValidator('start', { required: true, maxDaysAfter: 0, check: (value: any, obj: any) => {
        // TODO: Check that the start date is earlier than the end date
    }}),
    new DateValidator('end', { required: false, maxDaysAfter: 0 }),
    new StringValidator('name', { required: true })
];

let userRules = [
    new StringValidator('name', { minLen: 2, required: true }),
    new StringValidator('username', { required: true, format: { regex: /^[a-zA-Z0-9_-]{4,}$/, message: 'Invalid username' } }),
    new DateValidator('birthday', { required: false, maxDaysAfter: 0 }),
    new ArrayValidator('education', { required: true, minLen: 1, rules: eduRules })
];

Copyright © 2023 Ticatec. All rights reserved.

This library is released under the MIT License. For more details about the license, please refer to the LICENSE file.


Contact Information

huili.f@gmail.com

https://github.com/ticatec/web-bean-validator