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

@websublime/schema

v1.2.1

Published

Typescript data rules validation

Downloads

5

Readme

Schema

Schema is a validator for your data. You can define rules to check if the properties present on your schema are valid on the rules you have defined.

An express example:

const schemaObject = ObjectType<{ age: number; email: string }>({
  age: NumberType().min(18),
  email: StringType().isEmail()
});

let validation = await schemaObject.check({
  age: 19,
  email: "[email protected]"
});

expect(validation.properties?.age.hasError).toBeFalsy();
expect(validation.properties?.email.hasError).toBeFalsy();
class Parent {
  age: number;
  email: string;
}

class User {
  age?: number;
  email: string;
  parent: Parent;
}

const schema = ObjectType<User>({
  age: NumberType().min(18),
  email: StringType().isEmail(),
  parent: ObjectType<Parent>().shape({
    age: NumberType().min(50),
    email: StringType().isEmail()
  })
});

// schema.properties?.age.

const checkStatus = await schema.check({
  age: 17,
  email: "[email protected]",
  parent: { age: 40, email: "zicheng" }
});

expect(checkStatus.hasError).toBeFalsy();
expect(checkStatus.isValid).toBeFalsy();
expect(checkStatus.properties?.email.hasError).toBeFalsy();
expect(checkStatus.properties?.age.hasError).toBeTruthy();
expect(checkStatus.properties?.age.errors[0].i18n).toEqual(
  errorMessages.number.min
);

Table of contents

Usage

(Back to top)

This package as zero dependencies. It can work on any modern major browsers. Support for node will be added on the building system as well. The data to be validated can be any supported type of javascript types. Let's start for string type.

const str = StringType().minLength(5);

expect((await str.check("abcde")).hasError).toBeFalsy();
expect((await str.check("abcd")).hasError).toBeTruthy();

Now Number:

const validationSchema = NumberType().max(10);

expect((await validationSchema.check(9)).hasError).toBeFalsy();
expect((await validationSchema.check(11)).hasError).toBeTruthy();

Every type can have is particular checks, present in all are:

  • isRequired
  • isEmpty
  • addRule (to add new custom rules)

String

The rules defined on string type are:

  • containsLetter (check if value contains only letters)
  • containsUppercaseLetter (check if value is uppercase)
  • containsLowercaseLetter (check if value is lowercase)
  • containsLetterOnly (check if value contains letters only)
  • containsNumber (check if value constains numbers)
  • isOneOf (check if is one of the types included)
  • isEmail (check if is valid email)
  • isURL (check if is valid url)
  • isHex (check if is a hex value)
  • pattern (test a reg expression)
  • rangeLength (check if value is between minimum and maximum length)
  • minLength (check if value as minimum length)
  • maxLength (check if valu is less then maximum length)

Number

The rules defined on number type are:

  • isInteger (check if value is integer)
  • pattern (test a reg expression)
  • isOneOf (check if is one of the types included)
  • range (check if value is between the range)
  • min (check if value is equal or great to minimum value)
  • max (check if value is equal or lower to maximum value)

Boolean

The rules defined on boolean type are:

  • isRequired

Date

The rules defined on date type are:

  • range (check if date is beteewn min and max dates)
  • min (check if date is equal or greater than minimum date)
  • max (check if date is equal or lower than maximum date)

Object and Array

Object can be a compound of native and array a compound of native or objects.

Installation

(Back to top)

npm install @websublime/schema

Contributing

(Back to top)

Your contributions are always welcome! Please have a look at the contribution guidelines first. :tada:

Create branch, work on it and before submit run:

  • git add .
  • git commit -m "feat: title" -m "Description"
  • yarn changeset
  • git add .
  • git commit --amend
  • git push origin feat/... -f

License

(Back to top)

The MIT License (MIT) 2022 - Websublime. Please have a look at the LICENSE.md for more details.