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

json-schema-class

v0.0.1

Published

An Abstract Class that uses JSON Schema validation to build an extensible application with strongish type

Downloads

6

Readme

json-schema-class Build Status

codecov.ioCode ClimateesdocsIssue CountDependenciesdevDependency Status

Throughput Graphcodecov.io

Base Class that includes JSON Schema validations via ajv package.

Installing

npm install json-schema-class --save

Importing

import SchemaClass from 'json-schema-class';

What is SchemaClass able to do you ask?

SchemaClass Public Methods

You should be able to validate the class itself against the current schema draft. You can also store the model data in a class prop and validate it in the constructor/methods/other props.

It is able create a basic validator class

    

    //ES6
    let Validator = new SchemaClass({
      "type": "string"
    });

    // Using a valid input
    let text = Validator.validate("Input Text");
    expect(text).to.equal("Input Text");

    // Using an invalid input
    expect(function () {
      Validator.validate(1)
    }).to.throw(Error);
  

It is able to use extends

    
    class SimpleClass extends SchemaClass {
      constructor(text) {
        super({
          "type": "string",
          "enum": ["Text"]
        });

        this.validate(text);
      }
    }
    expect(SimpleClass).to.exist;

    let SimpleModel = new SimpleClass("Text");

    expect(SimpleModel).to.exist;
  

It is able to create new instances with defaults

    
    class WithDefaults extends SchemaClass {
      constructor(id) {
        super({
          "$schema": "http://json-schema.org/draft-04/schema#",
          "id": "WithDefaults",
          "type": "object",
          "properties": {
            "id": {
              "type": "string"
            },
            "check": {
              "type": "string",
              "default": "This is a default value"
            }
          },
          "required": ["id"]
        });

        this.id = id;

        this.validate(this);
      }
    }

    //On the import side you can instantiate it like so
    let defaultTest = new WithDefaults("1");

    //Expect find the class model's id
    expect(defaultTest.id).to.exist;

    //Any defaults should exist after creating a new instance
    expect(defaultTest.check).to.exist;
  

It is able to validate the model data

    
    class BadData extends SchemaClass {
      constructor(id) {
        super({
          "$schema": "http://json-schema.org/draft-04/schema#",
          "id": "BadData",
          "type": "object",
          "properties": {
            "id": {
              "type": "string"
            }
          },
          "required": ["id"]
        });

        /**
         * Do anything you need to do with the data model
         * You can create a custom Model placeholder or
         * validate the class directly like so
         *
         * let model = this;
         *  - or -
         * let model = {}
         */
        let model = this;
        model.id = 1;

        this.validate(model)
      }
    }

    expect(BadData).to.exist;

    expect(function () {
      new BadData(1)
    }).to.throw(Error);


  

SchemaClass Private Methods

Private methods for the SchemaClass are used to set and get the current schema for the class. Future plans are to expand this to include the caching features of ajv

It is able to throw a schema error using _schemaError

    
    let myObj = new SchemaClass();

    expect(myObj._schemaError).to.exist;
    expect(function () {
      myObj._schemaError()
    }).to.throw(Error);
    expect(function () {
      myObj._schemaError('Something Happening')
    }).to.throw(Error);

  

It is able to get and set the schema

    
    let myObj = new SchemaClass();

    expect(myObj.getSchema).to.exist;

    expect(function () {
      myObj.getSchema()
    }).to.throw(Error);

    expect(myObj._setSchema).to.exist;

    expect(function () {
      myObj._setSchema()
    }).to.throw(Error);

    expect(function () {
      myObj._setSchema({
        "type": "string"
      })
    }).to.not.throw(Error);

    expect(myObj.schema).to.deep.equal({
      "type": "string"
    });

    expect(function () {
      myObj.getSchema()
    }).to.not.throw(Error);

    expect(myObj.getSchema()).to.deep.equal({
      "type": "string"
    });


  

It is able to get and set a validator

    
    let myObj = new SchemaClass({
      "type": "string"
    });

    expect(myObj._getValidator).to.exist;

    let test = null;

    /**
     * The Validator will fail if setValidator is not triggered
     */
    expect(function () {
      test = myObj._getValidator()
    }).to.throw(Error);

    expect(function () {
      myObj._setValidator()
    }).to.not.throw(Error);

    expect(function () {
      test = myObj._getValidator()
    }).to.not.throw(Error);

    expect(test).to.exist;

    expect(myObj.schema).to.deep.equal({
      "type": "string"
    });

    expect(function () {
      myObj.validate("Input Text");
    }).to.not.throw(Error);

    expect(function () {
      myObj.validate(1);
    }).to.throw(Error);


    expect(function () {
      myObj._setValidator({
        "type": "number"
      })
    }).to.not.throw(Error);

    expect(function () {
      myObj.validate("Input Text");
    }).to.throw(Error);

    expect(function () {
      myObj.validate(1);
    }).to.not.throw(Error);

    expect(myObj.schema).to.deep.equal({
      "type": "number"
    });