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

@tongchia/jsschema

v0.2.12

Published

js schema

Downloads

8

Readme

JS-SCHEMA

Build Status npm NpmLicense

JS Schema validation library, compatible with json-schema. It is more suitable for js use, pursues concise and stylish code style, make it reusable as possible.

QUICK START

Install

install with npm

npm i -S '@tongchia/jsschema'

Usage

const {obj, str, num, int, date, arr, bool, buff, any} = require('jsschema');

const person = obj.properties({
  name      : str.maxLength(200).minLength(5),
  binary    : buff,
  living    : bool.default(true),
  updated   : date.default(Date.now),
  age       : int.min(0).max(65, true), // > 0 && <= 130
  email     : str.format('email'),
  birthday  : date.after('1890-01-01'),
  
  mixed     : any,
  _someId   : str.format('mongo-id'),
  _array    : arr,
  array     : [],
  _ofString : arr.items(str),
  ofString  : [str], // same as `array.items(string)` ↑
  ofNumber  : [num],
  ofDates   : [date],
  ofBuffer  : [buff],
  ofBoolean : [bool],
  ofMixed   : [any],
  ofObjectId: [str.format('mongo-id')],
  
  ofArrays  : [[]],
  ofArrayOfNumbers : [[num]],
  
  books     : [{ // => array.items(object.properties({...
    title     : str,
    author    : [str],
  }],
}).required(['name', 'age', 'birthday']);

person.isValid({
  name    : 'TongChia',
  age     : 30,
  birthday: new Date('10/18/1987')
}, (err) => {
  assert.ifError(err);
});

// to json-schema;
console.log(JSON.stringify(person))

VALIDATE

  • string
    • [x] enum
    • [x] pattern
    • [x] minLength
    • [x] maxLength
    • [x] format
      • [x] * from chriso/validator.js
        • alpha
        • email
        • url
        • base64
        • hex-color
        • md5
        • mongo-id
        • uuid
        • ip (ipv4, ipv6)
        • json
        • ...
      • [x] data-time (full-date, full-time)
      • [x] hostname
      • [ ] uri, iri
      • [ ] uri-template
      • [x] regexp
    • [ ] String-Encoding Non-JSON Data
  • number (integer)
    • [x] enum
    • [x] minimum, maximum
    • [x] exclusiveMinimum, exclusiveMaximum
    • [x] min, max (alias to minimum, maximum, exclusiveMinimum, exclusiveMaximum)
    • [x] range (alias to min, max)
    • [x] integer
    • [x] multipleOf
    • [ ] converter (numeric)
  • date (js-schema)
    • [x] after
    • [x] before
    • [ ] converter (date-time, full-date, date-string, time-stamp)
  • array
    • [x] minItems
    • [x] maxItems
    • [x] unique (uniqueItems)
    • [x] items
    • [x] additionalItems
    • [x] contains
    • [ ] entries ✨
    • [ ] Overload function ✨
  • object
    • [x] properties
    • [x] required
    • [x] patternProperties
    • [x] additionalProperties
    • [x] size (minProperties, maxProperties)
    • [x] propertyNames
    • [x] dependencies
      • [ ] schema dependencies
  • buffer (js-schema)
    • [ ] converter (strings, base64)
  • null (nil)
  • boolean
  • function (js-schema)
  • any
    • [x] allOf
    • [x] anyOf
    • [x] oneOf
    • [x] not
  • [x] Constant values
  • [x] Enumerated values
  • Metadata
    • [x] title
    • [x] description
    • [x] default
    • [x] examples
  • json-schema
    • [x] generate json-schema
    • [x] parse json-schema
      • [ ] Combining schemas ⚡️
  • referenced schema
    • [ ] $id ⚡️
    • [ ] $ref ⚡️
    • [ ] resolve method (browser & nodeJs) ⚡️

Custom validate

const {string} = require('jsschema');

string.addValidate(
  'keyword', // the keyword
  {
    validator: (value, params) => check(value, parameter), // return true/false;
    message: '`{value}` should valid for schema:{type}.{keyword}({params})',
  }
)

string.addValidate(
  'keyword',
  {
    isAsync: true,
    validator: (value, params, callback) => {
      // check value
      if ('ok')
        return callback(null, value)
      return callback(new ValidationError('...'))
    }
  }
)

TODO

  • [ ] custom validate;
  • [ ] test report;
  • [ ] automatic publish;
  • [ ] browser test;
  • [ ] toModel
    • [ ] MobX;
    • [ ] MongoDB;
      • [ ] mQuery;
  • [ ] mongoose schema;
  • [ ] json-schema;
    • [ ] parse json-schema;
    • [ ] generate json-schema;
    • [ ] test with ajv;
  • [ ] google protocol-buffers;
  • [ ] GraphQL;