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

@axetroy/struct

v2.2.1

Published

A Modern, Scalable , Graceful, Easy Use data structure validator

Downloads

27

Readme

Struct

Greenkeeper badge Build Status Coverage Status Dependency License Prettier Node npm version Size

A Modern, Scalable , Graceful, Easy Use data structure validator, Support browser and NodeJs

  • [x] All in Javascript. No Magic string.
  • [x] Strict mode, no one excess field.
  • [x] Most of type validator support.
  • [x] Scalable, easy to define your customize validator.
  • [x] Highly customizable.
  • [x] Validate with params, Support pass the argument to the validator.
  • [x] Pipe line, multiple validator work together.
  • [x] Support endless nest object, including Object and Array.
  • [x] Clear error message.
  • [x] Support nest Struct

Quick start

npm install @axetroy/struct --save
const { Struct, type } = require('@axetroy/struct');

const data = {
  name: 'axetroy',
  age: 18,
  address: {
    city: 'DC',
    code: '12' // invalid city code, it should be an integer
  }
};

const User = Struct({
  name: type.string,
  age: type.int,
  address: {
    city: type.string,
    code: type.int
  }
});

const err = User.validate(data);

console.log(err); // if all validator success, the error should be undefined

/**
{ Error
    at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/error.js:19:23)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/type.js:2:19)
    at Module._compile (module.js:635:30)
  validator: 'int',
  path: [ 'address', 'code' ],
  value: '12',
  detail: 'Expected a value of type `int` for `address.code` but received `12`.',
  message: 'Expected a value of type `int` for `address.code` but received `12`.' }
 */

Advanced usage

const { Struct, type } = require('@axetroy/struct');

const data = {
  name: 'axetroy',
  age: 18,
  address: {
    city: 'DC',
    code: 100
  },
  message: [
    { from: 'marry', msg: 'How are you?', timestamp: 1513155028 },
    { from: 'henry', msg: "How's going one?", timestamp: 1513135028 }
  ]
};

const User = new Struct({
  name: type.string,
  age: type.int.gte(18), // age is int && and age >= 18
  address: {
    city: type.string,
    code: type.int.gte(100)
  },
  message: [
    {
      from: type.string,
      msg: type.string,
      timestamp: type.int
    }
  ]
});

const err = User.validate(data);

console.log(err); // undefined, because the data pass the validator

Document

class: Struct

Create a struct

const { Struct, type } = require('@axetroy/struct');

const struct1 = new Struct(type.string);
const struct2 = Struct(type.string);

static Struct.define

static Struct.Type

struct.validate(data)

const err = Struct.validate({ word: 'Hello world' });

validate the data is match with struct, if all match. return undefined, if not, return an TypeError

class: Type

Create a type

static: Type.define(validatorName, handler)

  • validatorName:
  • handler: <(input):bool | (argv):(input):bool>
Type.define('email', function(input) {
  // here to check is it a email string
  return true;
});

define a customize type, will add an property on type.prototype

type.xxx

const stringType = type.string;
const intType = type.int;
const composingType = type.int.gte(100);

| Validator | Description | Require Argument | Source Code | | ----------------------- | ----------------------------------------- | ---------------- | --------------------------------------------------------------------------------------------- | | number | Check the type is a number | false | src/validator/number | | int | Check the type is a int | false | src/validator/int | | float | Check the type is a float | false | src/validator/float | | string | Check the type is a string | false | src/validator/string | | bool | Check the type is a bool | false | src/validator/bool | | any | Any type | false | src/validator/any | | odd | Check the type is a number and odd | false | src/validator/odd | | even | Check the type is a number and even | false | src/validator/even | | json | Check the type is json string | false | src/validator/json | | eq(value) | Equal to some value | true | src/validator/eq | | gt(number) | Greater then a number | true | src/validator/gt | | gte(number) | Greater then or equal a number | true | src/validator/gte | | lt(number) | Less then a number | true | src/validator/lt | | lte(number) | Less then or equal a number | true | src/validator/lte | | bt(min, max) | Between the min and max | true | src/validator/bt | | in(array) | The value is in the array | true | src/validator/in | | len(int) | The values's length property equal to xxx | true | src/validator/len | | msg(message) | Custom error message of this field | true | src/validator/msg | | func(validatorFunc) | Custom Validator | true | src/validator/func |

All the validator is define on type.prototype.

class: TypeError

  • validator: What validator fail
  • path: What key not pass the validator
  • value: The value which not pass the validator
  • message: The error message
  • detail: The error message

The TypeError inherit from Error

Examples

There is the examples, may be it can help you

Contributing

Contributing Guid

Contributors

| Axetroy💻 🐛 🎨 | | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |

License

FOSSA Status