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

@sa0001/api-validator

v0.0.14

Published

API-Validator is a library designed for validating input/output of API methods, with a very powerful but compact syntax.

Downloads

2

Readme

API-Validator

API-Validator is a library designed for validating input/output of API methods, with a very powerful but compact syntax.

Simple Usage

A line of code is worth a thousand words. Here's a carefully curated minimal example of a validator schema, and both passing and failing input.

var ApiValidator = require('@sa0001/api-validator')
var validator = ApiValidator.new() // use default options

var schema = validator.schema({obj:1,schema:{
	a: {int:1},
}})

var goodInput = {
	a: 1,
}
var badInput = {
	a: true,
}

// passing
result = schema.test(goodInput)

// failing
try {
	result = schema.test(badInput)
catch (err) {
	err
}

var output = ApiValidator.new().schema(schema).input(input).test()

Guiding Principles

  • the primary purpose of this library is for validating API input/output in JSON format, so don't count on values other than the following to be preserved: -- boolean -- number -- string -- array -- plain object
  • all other types of value, including errors, symbols, may not be preserved
  • only null can be JSON-stringified, so all the following values are considered the same as null: -- undefined, null,"", NaN, Infinity, -Infinity

Concepts

Simple Types: these are...

  • arr, array, isArray
  • bool, boolean, isBoolean
  • num, number, isNumber
  • obj, object, isObject
  • str, string, isString
  • any, mix, mixed -- allows any type of value, however most operators cannot be used

Complex Types: these are...

  • int, integer, isInteger

Operators: these are...

  • trim, toTrim, strip, toStrip (for Ruby lovers)
  • isRound
  • isLower, isLowerCase
  • isUpper, isUpperCase
  • isTrim, isTrimmed, isStripped (for Ruby lovers)

Descriptors: these are...

Example

	# example: sorted & unique array of enum values
	{arr:1,sort:1,uniq:1,vals:{str:1,in:SomeEnum}}
	
	# example: array of lat/lon
	{arr:1,schema:[
	    {num:1,isRound:8,btw:[-90,90]}
	    {num:1,isRound:8,btw:[-180,180]}
	]}
	
	# example: object of lat/lon
	{obj:1,schema:{
	    lat: {num:1,isRound:8,btw:[-90,90]}
	    lon: {num:1,isRound:8,btw:[-180,180]}
	}}
	
	# example: object of sorters (col -> dir)
	{obj:1,keys:{str:1,regex:/^[\w]+$/},vals:{str:1,in:['ASC','DESC']}}

TODO

  • operator: compact
  • operator: filter
  • operator: reject