bs

better-schemas

A simple schema validation implementation for NodeJs.

Showing:

Popularity

Downloads/wk

1

GitHub Stars

0

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

0

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

NPM
Build Status Coverage Status

better-schemas

A simple schema validation implementation for NodeJs.

Usage

const Schema = require('schema-js')

Create a schema for well-defined structures

const postSchema = new Schema('user.post', {
  name : { type: String, required: true }, // throws when not set
  description: String // optional
})

function handlePOST(req, res, next) {
  var data = req.body
  try {
    postSchema.validate(data)
    res.send('looks good!')
  } catch(e) {
    next(e)
  }
}

Define your own base-types

const MyType = new Schema.Type('my type', n => n == 1)
if(MyType.validate(1)) console.log('success!')

Define your own Schemas

const MySchema = new Schema('my-schema', {
  key : { type:Schema.Types.Any, default: 'value' }, // sets default when undefined
  arrays: [{type: String}],
  simple_types: Number,
  helper_types: Schema.Types.Integer,
  name : { type:String, required: true } 
})

var data = MySchema.validate({ key: undefined, arrays: ['are', 'great'] }) // undefined & null are replaced by defaults

Retrieve schemas by name

const MySchema = Schema.get('my-schema')

Create a nested Schema

const MySchema = Schema.get('my-schema')
const MyOtherSchema = new Schema('complex-schema', {
  name: { type: String, required: true },
  data: MySchema
})

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial