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

shallow-schema

v1.1.6

Published

A lightweight package for using simple JSON schemas.

Downloads

5

Readme

shallow-schema

A lightweight package for using simple JSON schemas.

Install

npm install shallow-schema

Usage

Requiring the module:

const shallowSchema = require('shallow-schema')

Testing a schema: testSchema

const { testSchema } = require('shallow-schema');

const s1 = { "name": { type: 'string' } };
const s2 = { "name": { type: 'strong' } };

console.log( testSchema(s1) ); // true
console.log( testSchema(s2) ); // false ('strong' is not a valid value for <type>)

Testing a source object: assertSchema

const { assertSchema } = require('shallow-schema');

const schema = { "name": { type: 'string' } };
const o1 = { name: 'alex' };
const o2 = { name: 9 };
const o3 = { Name: 'alex' };

console.log(assertSchema(o1)); // true
console.log(assertSchema(o2)); // false ('name' property is not of type 'string')
console.log(assertSchema(o3)); // false ('Name' property was not defined in schema)

API Reference:

testSchema:

testSchema(schema: Object): boolean

  • Receives:
    • schema - An object to test for schema validity
  • Returns:
    • boolean - Whether or not 'schema' parameter is a compliant schema

assertSchema:

assertSchema(schema: Object, source: Object): boolean

  • Receives:
    • schema - The schema to use as comparison basis
    • source - An object to compare
  • Returns:
    • boolean - Whether or not 'source' parameter matches the 'schema' configuration
  • Exceptions:
    • If the schema passed is not valid, and "Invalid Schema" error will be thrown.

Schema Construction Reference:

Structure:

A valid schema is an object containing one or more property declarations, containing the necessary fields.

const s = {
	property_name: {
		"type": string,
		"optional": boolean,
		"pattern": string,
		"min": number
		"max": number,
		"values": array,
		"test": function
	};
};

Fields:


"type"

Type: String (one of: "string" | "function" | "array" | "number" | "any" | "boolean" | "object") Omittable: No

This property is mandatory, and specifies the type of the property defined. It may be set to 'any' if the value of the property is variable.


"optional"

Type: Boolean Omittable: Yes Default: false

The 'optional' field can be set to true to indicate that the property in the scheme does not need to be defined in any compared objects.


"pattern"

Type: String Omittable: Yes

This generates different effects according to the property type:

  • string: The whole string is compared.
  • array: All the array items are compared.
  • object: All the object values are compared.
  • Others: This option is ignored.

"min"

Type: Number Omittable: Yes

Compares the value of a property with this as minimum (inclusive).

This generates different effects according to the property type:

  • string: The string length is compared.
  • number: The number value is compared.
  • array: The array length is compared.
  • object: The number of properties in the object is compared.
  • Others: This option is ignored.

"max"

Type: Number Omittable: Yes

Compares the value of a property with this as maximum (exclusive).

This generates different effects according to the property type:

  • string: The string length is compared.
  • number: The number value is compared.
  • array: The array length is compared.
  • object: The number of properties in the object is compared.
  • Others: This option is ignored.

"values"

Type: Array Omittable: Yes

It is an array containing any number of items of any type. Checks if the value of the property matches one or more items in this array (case sensitive)


"test"

Type: Function Omittable: Yes

It is a callback function that receives a parameter "value". Checks if the function call with the property compared as argument returns true.


Example:

A schema that checks for an email, password and an optional age:

const s1 = {
	email:{
		type: 'string',
		pattern: '^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+\.([a-zA-Z]+)?$'
	},
	password: {
		type: 'string',
		// Password min length is 8
		min: '8',
		// Password max length is 20
		max: '20',
		// Password must include a special char
		test: (value) => (/\!\@\#\$\&\*/).test(value)
	},
	age: {
		optional: true,
		type: 'number',
		// Age must be higher or equal 17 and lower than 100 (up to 99).
		min: 1,
		max: 100
	}
}