npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

appily-validate

v0.5.0

Published

Validate a model based on rules

Downloads

3

Readme

appily-validate

Validate a model based on rules.

Built for appily an application generator but can be used by any project to validate forms or other data.

  • Designed to be used in front and backend code
  • No dependencies
  • Supports JSON rules
  • Full model validation allows complex rules
  • Supports condtional validation
  • Supports internationalization (i18n)

Getting started

$npm install appily-validate

How to use

validates a javascript object given a set of rules

import { validate, Validator } from 'appily-validate';

const rules = [
  { type: Validator.NotEmpty, property: 'name', message: 'required' }
];
const results = validate(rules, { color: 'red' });

/*
results = { 
  valid: false, 
  properties: { 
    name: { 
      valid: false, messages: ['required']
    }
  }
}
*/

Validators:

NotEmpty

Checks if a property on the given object is not empty

const rules = [
  { type: Validator.NotEmpty, property: 'name', message: 'required' }
];

Message can be a string, object, code or i18n key

RegEx

Checks if a property matches a regEx, regEx can be a literal or a string

const rules = [
  { type: Validator.RegEx, property: 'name', regEx=/[A-Z][a-z]*/ message: 'Name must start with an uppercase' }
];

Message can be a string, object, code or i18n key


Comparison (=, <, <=, >=, >, !=)

Compares a property to another property or constant value

const rules = [
  {
    type: Validator.Comparison,
    operator: '==',
    property: 'confirmPassword',
    compareTo: 'password',
    message: 'Must match password'
  },
  {
    type: Validator.Comparison,
    op: '<',
    property: 'weight',
    value: 250,
    message: 'must be less than 250'
  }
];

Supported operators are: =, <, <=, >=, >, !=

Message can be a string, object, code or i18n key


Length

Checks the length of a property. Can use min, max and is. Can just set min or max if there is only one boundry.

const rules = [
  {
    type: Validator.Length,
    property: 'zip',
    is: 5,
    message: 'Zip must be 5 characters'
  },
  {
    type: Validator.Length,
    property: 'name',
    min: 2,
    max: 100,
    message: 'must 2-100 characters'
  }
];

Message can be a string, object, code or i18n key


And

Is true if all rules are true.

const rules = [
  {
    type: Validator.And,
    property: 'age',
    message: 'must be between 0 and 120',
    rules: [
      { type: Validator.Comparison, op: '>', property: 'age', value: 0 },
      { type: Validator.Comparison, op: '<', property: 'age', value: 120 }
    ]
  }
];
  • rules: can use any validator
    • messages in inner rules are ignored
  • message: can be a string, object, code or i18n key

Or

Is true if one of the rules is true.

const rules = [
  {
    type: Validator.Or,
    property: 'order',
    message: 'must be chicken, beef or veggie',
    rules: [
      {
        type: Validator.Comparison,
        op: '==',
        property: 'order',
        value: 'chicken'
      },
      {
        type: Validator.Comparison,
        op: '==',
        property: 'order',
        value: 'beef'
      },
      {
        type: Validator.Comparison,
        op: '==',
        property: 'order',
        value: 'veggie'
      }
    ]
  }
];
  • rules: can use any validator
    • messages in inner rules are ignored
  • message: can be a string, object, code or i18n key

Not

Is true if rule is false.

const rules = [
  {
    type: Validator.Not,
    property: 'name',
    message: 'must not be Steve',
    rule: {
      type: Validator.Comparison,
      op: '==',
      property: 'name',
      value: 'Steve'
    }
  }
];
  • rule: can use any validator
    • the message in the inner rule is ignored
  • message: can be a string, object, code or i18n key

Condition

Is the results of then if the condition is true

const rules = [
  {
    type: Validator.Condition,
    property: 'hasCarInsurance',
    message: 'required',
    condition: {
      type: Validator.Comparison,
      op: '==',
      property: 'ownsCar',
      value: true
    },
    then: {
      type: Validator.Comparison,
      op: '==',
      property: 'hasCarInsurance',
      value: true
    }
  }
];
  • condition: can use any validator
    • the message in the inner rule is ignored
  • then: can use any validator
    • the message in the inner rule is ignored
  • message: can be a string, object, code or i18n key

More validators are coming soon


Custom Validators

addValidator can be used to add custom validators

addValidator takes a validator function with this signature

{ property, value, rule, model } => Boolean

Custom validators are global and can be resused throughout your app

import {addValidator, validate} from 'appily-validate'

//Make your custom validator
function eggStyleValidator({ value }) {
  const styles = [
    'poached',
    'sunny side up',
    'scrambled',
    'over easy',
    'hard boiled'
  ];

  return styles.includes(value.toLowerCase());
}

validateOrder(order) {
  //Register your custom validator
  addValidator('EggStyle', eggStyleValidator);
  const rules = [
    {
      type: 'EggStyle', //use your custom validtor in a rule
      property: 'eggStyle',
      message: 'Not an egg style option'
    }
  ];

  return validate(rules, model);
}

TODO

  • Add more validators
    • Range
    • DateRange
    • Min
    • Max
    • Object
    • Array
    • Contains
  • Common regex
    • phone
    • email
    • zip
  • Throw error on unfound validator
  • Add multi rule tests
  • Add global rules