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

@sergtyapkin/models-validator

v1.0.8

Published

![Run tests](https://github.com/SergTyapkin/js-models-validator/workflows/Run%20tests/badge.svg) ![downloads](https://img.shields.io/npm/dt/%40sergtyapkin%2Fmodels-validator)

Downloads

186

Readme

Models validator

Run tests downloads

Models Validator for Objects. For example, for JSON-parsed Objects that received from network.


Docs for models:

All model fields describes as: from: fieldType

fieldType can be declared as:

  • Simple type: String, Number, Date, Object, Array,...

    Array in this case can contain any elements of any types

  • Enum: new Set(["some_val", 456, "val2"]) (Field can only be "some_val" or 456 or "val2")
  • Specialized Array (fixed length): [Number, String, String] (specialized type of every element in array)
  • Specialized Array (unlimited length):
    {
      type: Array,
      item: /**{{fieldType}}**/,
    }
  • Long type declaration (you can set optional param):
    {
      type: /**{{fieldType}}**/,
      optional: true, // |=>  field will not be exists in result object if it's not provided
      default: "SomeDefaultValue", // => If field not provided in source data, it will have this value
      from: "some_name", // |=>  field will be searched in the source model as a field with the name "some_name" 
    }
  • Nested object declaration:
    {
      type: Object,
      fields: {
        // from: fieldType,
        // ...    
      }
    }

Fields options at long declaration as a result:

  • type {{fieldType}} - Describes the type to which the original field value is converted.
  • optional [Boolean] - If it has value "false", field can be not provided.
  • default [Any value that converts to {{fieldType}}] - If field is optional and it's not provided in source data, it will have this value.
  • from [String] - Name of field with which it will be searched in the source model
  • item {{long or short fieldType}} - If field type is Array, it can be long or short declaring of field type.
  • fields {Object} - If field type is Object, it must be Object with long or short declaring of each object field.

Example model description:

const exampleModel = {
  field1: String, // |=> "some_string"
  field2: Number, // |=> 123.4123
  field3: Object, // |=> {any_fields: any_values, ...}
  field4: Array,  // |=> [any_types, ...]
  field5: [Number, String], // |=> [123.1244, "some_string"]
  field6: new Set(["some_val", 456, "val2"]), // |=> Enumeration. Can only be "some_val" or 456 or "val2"
  field7: {
    type: String
  }, // Equals as `field7: String`
  field8: {
    type: Object,
    fields: {
      // ... Any fields of nested model, for example:
      field8_1: String,
    },
  },
  field9: {
    type: Number,
    optional: true, // |=>  field will not be exists in result object if it's not provided
  },
  field10: {
    type: Array,
    item: String, // |=> short or long declaration of each field in array
    from: "some_field_10", // |=> field will searched as field "some_field_10" and written in "field10"
  },
  field11: {
    type: Array,
    item: {  // example of long declaration of each field in array:
      type: Object,
      fields: {
        field11_1: String,
      }
    },
  },
}

Example model validation:

const UserModel = { // declare model
  age: Number,
  userName: {
    type: [String, String], // maybe ["name", "surname"]
    from: "user_name", // name in API model
  }, 
  sex: new Set(['male', 'female']), // one of two values
  children: {
    type: Array, // array with unlimited length
    optional: true, // mey be not exists
    item: {
      type: Object,
      fields: {
        name: String,
        age: Number,
      }
    }
  }
}

const response = fetch('/user', {method: 'GET'}); // get unvalidated JSON data
const data = validateModel(UserModel, await response.text()); // validate

For more examples you can see file with tests validateModel.test.js