guard-me
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

Work is still in progress!

Build Status npm version

guard-me

guard-me is next-generation validation library for node.js.

Features:

  • Decouples your validation from the rest of the code
  • Written in TypeScript. This means Definition Files (.d.ts) files are bundled within the package

Built-in validators:

  • equal
  • not equal
  • required
  • length (min, max)
  • Do anything you want with custom validation! Supports Sync and Async validations using Promises
var ensure = require('guard-me').ensure
 
var guard = ensure.that((check, object) => {
  check(object.title).required().length(1, 20)
  check(object.slug).required().message("Slug is mandatory! It's currently empty")
  check(object.tags).length(1, 3)
})
 
//Example A
var request = {
  title: 'Garmin Swim',
  slug: 'garmin-swim',
  tags: ['watch', 'garmin', 'sports']
}
 
guard.check(request).then((result) => {
  console.log(result.valid); //output: true
})
 
//Example B
var request = {
  title: 'Garmin Swim',
  slug: '',
  tags: []
}
 
guard.check(request).then((result) => {
  console.log(result.valid); //output: false
 
  console.log(result.errors[0].property); //output: slug
  console.log(result.errors[0].messages[0]); //output: Slug is mandatory! It's currently empty
 
  console.log(result.errors[1].property); //output: tags
  console.log(result.errors[1].messages[0]); //output: Tags must have between 1 and 3 elements
})

Package Sidebar

Install

npm i guard-me

Weekly Downloads

0

Version

0.0.8

License

GPL-3.0

Last publish

Collaborators

  • goenning