2.5.6 • Published 2 years ago

tegund v2.5.6

Weekly downloads
2
License
MIT
Repository
-
Last release
2 years ago

English | 简体中文

Introduction

tegund is a very simple and intuitive JavaScript type checking library. Features overview:

  • Small size: The complete size is only 4.9kb (after gzip compression), and does not rely on other third-party libraries;
  • Rich types: Provide dozens of basic types;
  • Easy to expand: Allows you to differentiate new types from basic types;

Example

Basic use

const {object, string, number, integer, date, array} = require('tegund')

// basic type check
string().check('abc') // true
number().check(true) // false

// addtional condition
string().min(5).check('abc') // false

// object
object().check({}) // true
object().check([]) // false

// group
const group = object({
  name: string(),
  age:'integer' // use alias, equal to integer()
})

group.check({}) // false
group.check({ name:'leo', age: 18 }) // true

// array
array().check([1,'abc', true]) true

// typed array
array(string()).check(['abc', 1]) // false
// or use alias
array('string').check(['abc', 1]) // false

// a array containe string or number
array('string','number').check(['abc', 1]) // true

Below we use tegund to verify more complex scenarios. Suppose we want to create a user table and use tegund to verify the input:

const {object, string, integer, date, array} = require('tegund')

const interface = object({
  name: string().min(2).max(10), // the user name must be a string and at 2-10
  age: integer().positive(), // age must be a positive integer number
  address: object({ // the address is a object
    province: string(),
    city:'string', // use alias, it is same at string()
    county:'string',
    addressDetail:'string',
  }),
  email: string().pattern( // you can add addtional pattern and error message
    /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
    'Please enter a correct email address'
  ),
  pwd: string().pattern(
    /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/,
    'Please enter a password with more than 8 digits, including numbers and letters'
  ),
  tag: array('string'), // tag must be a array<string>
  remark: string().optional(), // this field is optional
  createTime: date().forbid(), // can not provide a createTime prop
  updateTime: date().forbid(),
})

// then validate the params
const args = {}
// test it, is pass, get a undefined, if not, will get a ValidateError
const err = interface.test(args)
// if pass, will get a true
const passed = interface.check(args)

// or you can use assert
// if not pass, will throw a error
interface.assert(args)

Error message:

>>>>> interface.assert()
<<<<< ValidateError: None is not a Object type

>>>>> interface.assert({})
<<<<< ValidateError: field name validate error, expected a String type, got a [object Undefined]

>>>>> interface.assert({name:''})
<<<<< ValidateError: field name validate error, expected a String, length gte than 2, but got a length: 0

>>>>> interface.assert({name:'leo'})
<<<<< ValidateError: field age validate error, expected a Integer type, got a [object Undefined]

// and so on...

For detailed usage, please check the Document

Installation and use

# Install tegund
npm i tegund

License

MIT

Copyright (c) 2021-present aim-leo

2.5.6

2 years ago

2.5.5

2 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.3.0

3 years ago

2.5.0

3 years ago

2.3.2

3 years ago

2.2.3

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.3.3

3 years ago

2.2.6

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago