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

web-schemas

v0.1.3

Published

A schema parser for web.

Downloads

5

Readme

#web-schemas

A schema parser for web.

Installation

npm install web-schemas

Usage

// Koa/Express app configuration...
// blabla...

var Schema = require('web-schemas'),

var fooSchema = new Schema({
    foo: Schema.int.gt(3).lt(5).required(), // foo should between 3~5, and not undefined.
    bar: {
        a: [{ val: Schema.string.len(3, 8) }], // val.length should between 3~8.
        b: { val: Schema.date } // val is a Date object.
    }
});

// { foo: '4', bar: { a:[ { val: '6' } ], b: '2014-01-30' } will be parsed as { foo: 4, bar: { a: [ { val: 6 } ], b: Date('2014-01-30') } }

// koa
app.use(route.get('/foo', function *() {
    this.body = fooSchema.parse(this.query);
}));

// express
app.get('/foo', function (req, res) {
    try {
        var data = fooSchema.parse(req.query);
        res.jsonp(data); // If fooSchema.sanitize success, there will be a 'data' field contains sanitized data.
    } catch (e) {
        res.jsonp(500, e.path + ': ' + e.message); // Echo what's wrong.
    }
});

Schemas

  • int
  • number
  • string
  • date
  • bool
  • ObjectSchema: create by new WebSchema({ / blabla / })
  • ArraySchema: create by new WebSchema({ foo: [/ blabla /] }) or new WebSchema.Array({ blabal })

Chaining

Every schema type have chaining methods to add extra validate conditions/sanitize methods.

Common

  • default(val) - if result is undefined, use val instead.
  • in(arr) - arr.indexOf(result) >= 0.
  • notIn(arr) - arr.indexOf(result) < 0.

ObjectSchema

  • required() - required.

ArraySchema

  • required() - required.
  • len(min, max) - min <= array.length <= max.

IntSchema / NumberSchema / DateSchema

  • required() - required.
  • gt(val) - result > val.
  • gte(val) - result >= val.
  • lt(val) - result < val.
  • lte(val) - result <= val.

StringSchema

  • required() - required.
  • notEmpty() - result !== '' && result !== undefined.
  • len(min, max) - min <= string.length <= max.
  • is(pattern) - pattern.test(string) === true.
  • not(pattern) - pattern.test(string) === false.
  • contains(substr) - string contains substr.
  • notContains(substr) - string not contains substr.
  • isAlpha() - string is alpha.
  • isAlphanumeric() - string is is alpha or numeric.
  • isNumeric() - string is is numeric.
  • isHex() - string is hex numberic.
  • isHexColor() - string is css hex color.
  • isLowerCase() - string is in lower case.
  • isUpperCase() - string is in upper case.
  • isUUID() - string is UUID
  • isUUIDv3() - string is UUIDv3
  • isUUIDv4() - string is UUIDv4
  • isUUIDv5() - string is UUIDv5
  • isMd5() - string is MD5.
  • isObjectId() - string is mongo ObjectId.
  • isEmail() - string is Email.
  • isUrl() - string is URL.
  • isMacAddress() - string is MAC address.
  • isIPv4() - string is IPV4.
  • isIPv6() - string is IPV6.
  • toLowerCase() - convert result to lower case.
  • toUpperCase() - convert result to upper case.