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

fn-validator

v2.1.0

Published

Extendable and Isomorphic functional schema validator for browser and Node.js

Downloads

5

Readme

fn-validator

Extendable and Isomorphic functional schema validator for browser and Node.js


Motivation

JavaScript being dynamic and weakly typed language, does not support anything which can help you validate the JSON object structure.

However, writing business logic in NodeJS, or even writing a browser based application requires strong type object contract interface to validate the data coming from or going to external sources and acknowledge the error messages in case of mismatch. Now there are some JSON schema standards and libraries out there which help you do that but they lack the extensibility in terms of defining new kind of validators or composing them to make hybrid validators.

This library aims at

  1. Defining flexible and tiny architecture to plug in any kind of reusable custom validator.

  2. Using JavaScript as functional language without side effects. All validators are pure functions.

  3. Providing facility to compose different kind of validators along with custom error messages.

  4. Being predictable with full test coverage.


Install

npm install fn-validator --save


Use

var v = require("fn-validator");

var userSchema = v.object({
   firstName : v.string,
   lastName : v.all([v.string, v.minLength(2), v.maxLength(30)]),
   gender : v.enum(["Male", "Female", "Other"]),
   age : v.number,
   height : v.between(160, 190),
   graduationDate : v.before(new Date("2000-01-01")),
   single : v.boolean,
   hasGirlfriend : v.optional(v.boolean)
 })

 var schema = v.object({
     orgName : v.string
     users : v.arrayOf(userSchema)
 })

var isValid = v.validate(schema, objectToBeValidated);

if(!Object.isEmpty(result))
  //handle error
else
  // all good

Custom Validator

If there is an error, Return String value which is a error message

Return true or don't return anything at all if value is valid

Define Validator


function customValidator(key, value, object) {
  var errorMessageString = "Value is not valid";
  if(value is not valid) {
    return errorMessageString;
  }
//  else
//    return true;
}

Define schema


var schema = object({
  attribute: customValidator
})

Validate

expect(v.validate(schema, objectToBeValidated)).toEqual({
  error_attribute : "Value is not valid"
})