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

xjot

v1.0.13

Published

A extensible object type checking library

Downloads

6

Readme

xjot: An extensible object type validation tool

Overview

xjot is a type validation library to accelerate the data validation in RESTful APIS.

To use xjot:

  • Define your object template
  • Use the template to validate objects

Basic use

    let contactT = xjot.object {
        firstName : xjot.string( { required: true, maxLength: 18, autoTruncate: true }),
        lastName : xjot.string( { required: true, maxLength: 18, autoTruncate: true }),
        age: xjot.int( required: false, min:1, max: 77 )
    }    

    myObject = { firstName: "John", lastName = "Doe"}

    console.log(JSON.stringify(contactT.validate(myObject),undefined,4 ))

xjot will not only check the field values. It will also perform type convesion and modify the values based on the formatting parameters provided.

Template reference

Creating a template

A template is an object that contains describes what rules objects of this kind must follow. A template is different from a class in that a class describes how to create new instances of an object. A template describes the validation rules for the data in that object, as well as some transformations to be applied to the data during validation.

  // For simple types
  myfield = xjot.<type>( {rules} )

  // For object 
  myTemplate = xjot.<type>( {fieldsDefinition} [, {rules}] )

Types

xjot currently supports the following types:

  • int: integer (value will be truncated) or string that can be converted to a number
  • number: any number or string that can be converted to a number
  • boolean: any value that can be interpreted as a boolean (true, false, Y, N, 1, 0)
  • date: a string that meets the defined criteria, or string that can be converted to a date
  • string: a string that meets the defined criteria
  • object: an object composed of any of the xjot types
  • array: and array of any of the xjot types

Template rules

  • required: indicates if a field must be present and not undefined (Default false)
  • default: when undefined, default value to use
  • acceptNull: true if a "null" value is accepted (Default false)
  • min, max: boundaries for numbers
  • maxLength: maximum length for string values
  • autoTruncate: if true, string value will be truncated to maxLength
  • values: an list of accepted values in an array
  • dbfield: mapped SQL database field for automatic SQL generation (Applies to simple types).
  • autoIncrement: indicates the underlying database field is not writable.
  • primaryKey: indicates this field is to be used ar primary key in update SQL.
  • table: mapped SQL database table for automatic SQL generation (Applies to object types ).

Object definitions

The object template definition is an object whose keys are the field name of the objects to check and the values are instances of the field types

Example

    let addressT = xjot.object({
      line1:   xjot.string( { required: true, maxLength: 80, autoTruncate: true ),
      line2:   xjot.string( { required: false, maxLength: 80, autoTruncate: true ),
      line3:   xjot.string( { required: false, maxLength: 80, autoTruncate: true ),
      zipcode: xjot.string( { required: true, maxLength: 20, autoTruncate: true ),
      city:    xjot.string( { required: true, maxLength: 50, autoTruncate: true ),
      state:   xjot.string( { required: false, maxLength: 30, autoTruncate: true ),
      country: xjot.string( { required: true, maxLength: 30, autoTruncate: true ),
    })

    let contactT = xjot.object ({
        firstName : xjot.string( { required: true, maxLength: 18, autoTruncate: true, dbfield: "first_name" }),
        lastName : xjot.string( { required: true, maxLength: 18, autoTruncate: true }),
        age: xjot.int( required: false, min:1, max: 77 ),
        address: addressT
    }, { table: "contacts" })