1.2.4 • Published 5 years ago

@onedaycat/ts-validator v1.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

npm (scoped) GitHub Coverage Status Build Status

ts-validator

Application validator. Internally uses validator.js to perform validation.

Install

npm install @onedaycat/ts-validator

or

yarn add @onedaycat/ts-validator

Usage

import Validator, { isEmail } from '@onedaycat/ts-validator'

const schema = {
  email: [isEmail()],
}
const values = {
  email: 'john.doe@mail.com',
}

// throw error
Validator.test(schema, values)

Example

Main.ts

Customizing the error messages (optional).

import Validator from '@onedaycat/ts-validator'

Validator.message = {
  ...Validator.message,
  min: 'YOUR_MESSAGE'
  max: 'YOUR_MESSAGE'
}

SignInInput.ts

import Validator, { isEmail, min } from '@onedaycat/ts-validator'

export default class SignInInput {
  constructor(
    public email: string,
    public password: string,
  ) {}

  public validate() { 
    const schema = {
      email: [isEmail()],
      password: [min(10)]
    }

    const values = {
      email: this.email,
      password: this.password,
    }

    // throw error
    Validator.test(schema, values)
  }
}

Validators

ValidatorsDescription
min(value: number)check if the number is less than or equal to minimum.
max(value: number)check if the number is more than or equal to maximum.
isEmail()check if the string is an email.
isEmpty()check if the string has a length of zero.
isNotEmpty()check if the string has length more than zero.
compareWith(field: string)check if the value matched with the expected field.
minLength(value: number)check if the string has length less than or equal to minimum.
maxLength(value: number)check if the string has length more than or equal to maximum.

Contributing

  1. Fork this repository.
  2. Create new branch with feature name in format feature/FEATURE_NAME
  3. Run npm install or yarn
  4. Create your feature.
  5. Commit and set commit message with feature name.
  6. Push your code to your fork repository.
  7. Create pull request.

Licence

MIT

1.2.4

5 years ago

1.2.3

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago