ts-validation

@bshg/validation

visit our docs site: Bshg Docs

Welcome to @bshg/validation, a versatile TypeScript library crafted for seamless data validation within your projects. Whether you’re working on a front-end or back-end application, this library empowers you to validate data in a declarative manner, ensuring your objects align with your expectations.

Designed with simplicity and efficiency in mind, @bshg/validation streamlines the validation process, making it a reliable choice for your projects. It offers extensive customization options, enabling you to tailor validation rules and error messages to fit your specific requirements with ease.

This library is lightweight and has no external dependencies, ensuring fast load times and minimal impact on your application’s performance. Whether you’re building a web application, API, or mobile app, you can rely on @bshg/validation to handle validation consistently across platforms.

Why Choose @bshg/validation?

Getting Started

Let’s dive into the details of how to use this library effectively.

Installation

Install @bshg/validation via npm:

npm install @bshg/validation
yarn add @bshg/validation

Basic Usage

  1. Creating Your Type First, define the TypeScript class for your data model:
     export class User {
       username: string;
       password: string;
       fullName: string;
       age: number;
       email: string;
       phone: string;
     }
    
  2. Import the Library Import the v symbol, which contains all the library methods you can use:
     import { v } from '@bshg/validation';
    
  3. Creating a Validator for Your Type Create a validator instance for your User type:
     import { v } from '@bshg/validation';
        
     const item = new User();
        
     const validator = v.validator<User>({
       items: {
         username: v.string().required().alpha(),
         password: v.string().required().min(8),
         fullName: v.string().required(),
         email: v.string().required().email(),
         phone: v.string().required().phone(),
         age: v.number().positive(),
       }
     }).init(() => item);