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

yuschema

v1.0.2

Published

Object schema definition for data validation and schema compatibility checks

Downloads

3

Readme

Schemas

Object schema definition for data validation and schema compatibility checks

Usage

Schema definition

The library has a helper to create a schemas, schemas are created using the schema class

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'string'
})

Currently the available types are the following : email number integer url object array boolean, each schema type has its own rules, and will be described further down.

string

The string schema represents a string, it allows for additional validation rules

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'string',
    validators : [
        {
            min : 0,                // minimum string length
            max : 10,               // maximum string length
            regex : '^(a-Z)+$'      // regular expression for validation
        }
    ]
})

number

The number schema represents a number, it allows for additional validation rules

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'number',
    validators : [
        {
            min : 0,                // minimum value
            max : 10,               // maximum value
        }
    ]
})

integer

The number schema represents an integer, it allows for the same validation rules than number

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'number',
    validators : [
        {
            min : 0,                // minimum value
            max : 10,               // maximum value
        }
    ]
})

email

The email schema represents an email and has currently no additional properties

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'email',
})

boolean

The boolean schema represents a boolean and has currently no additional properties

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'boolean',
})

object

The object schema is a bit particular since it contains child schema and allows to represent more complex structures

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'object',
    properties : {
        name : { type: 'string' },              // to define a property name of type strings
        other_prop : { type : 'number'}         // to define a property other_prop of type strings
    }
})

array

The array schema is a bit particular since it contains child schema and allows to represent more complex structures

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema({
    type : 'array',
    arrayType : { type : 'string' }     // to define an array of strings 
})

Custom types

Sometimes you might want to create more complex data structure like a three for example, for this we created the definition options.

// example of string definition
import {Schema} from 'yuschema'

const schema = new Schema<'node'>({         // We extend the allowed types
    definitions : {
        node : {                            // we define a new type 'node'
            type : 'object',                // a node represents an object
            properties : {
                children : {
                    type : 'array',
                    arrayType : { type : 'node' }      // the schema might reference itself
                }
            }
        }
    } 
    type : 'object',
    properties : { 
        head : 'node'                   // we reference the head of the three 
    } 
})

Validation of values

Schema allow you to validate values by checking if they correspond to the schema, for objects and arrays validation occurs recursively

// example 1

new Schema({ type : 'string' }).validate('this is a string') // will return true
new Schema({ type : 'string' }).validate(5) // will return false

Schema comparison

The library was primarily build to compare schemas and to check compatibility. In order to allow this we introduced the concept of specificity

Specificity

Specificity is a way to determine if a schema is more accurate than another one in its definition, for example the type email is more specific than string or the type integer is more specific than the type number.

Specificity allows applies for strings and more complex types like a string with a regexp is more specific than as string without regex, or a string with a min length of zero is more specific that a string with a min length of 2 (this is debatable for strings, but in our use case, where we compare strings to insert them into a database for example it makes sense). For Objects we call the most specific object the one that has the most parameters defined and for which is parameter is defined more specifically.

In order to compare schema you can use the following methods:

// example 1

new Schema({ type : 'string' }).isMoreSpecificThan(new Schema({ type : 'email' })) // will return false
new Schema({ type : 'string' }).isMoreGeneralThan(new Schema({ type : 'email' }))  // will return true

Install

The package is types, in order to install it just enter the following command line

yarn add yuschema

or

npm install yuschema