0.1.0 • Published 5 years ago

obeyman v0.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Obeyman

A simple object schema validate tool. (Simplified version of hapijs/joi)

version dependencies npm.io npm.io

Getting Started

Install

via npm

npm i obeyman -S

Usage

// define schema
const schema = Obeyman.object().keys({
    userId: Obeyman.number().required(),
    pageNum: Obeyman.number().required(),
    pageSize: Obeyman.number().optional(),
    ts: Obeyman.string(),
});

const target = {
    userid: 123,
    pageNum: 2,
    ts: +new Date()
};

// validate
const errStack = Obeyman.validate(target, schema);
console.log(errStack);
// output: 
//  [
//      { userId: 'is required' },
//      { ts: 'this field should be string' }
//  ]

// or use callback
Obeyman.validate(data, schema, (error, stack) => {
    if (error) {
        console.warn(`Validate failed\n`, stack);
    }
});

API Reference

Other types(e.g. array,number,string) inherit from type any

  • #any
    • any.allow
    • any.required
    • any.optional
    • any.len
    • any.min
    • any.max
  • #array
    • array.items
  • #boolean
  • #number
  • #object
    • object.keys
  • #string
    • string.alphaNumber

see joi API Reference doc(8.0.5)

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago