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

big-schema

v0.5.2

Published

A data validation library with support for complex schemas.

Downloads

3

Readme

Robust Data Validation for TypeScript

Big-Schema is a TypeScript library designed for robust schema-based data validation. Using Big-Schema, you can easily define what your data should look like and ensure that it fits the mold. It is perfect for validating API requests, user inputs, configuration files, or any other structured data.

Features

  • Type-First: Schema definitions that create TypeScript types.
  • Comprehensive: Supports strings, numbers, booleans, arrays, objects, dates, and even unions.
  • Type-safe: Leverage TypeScript's type system for maximum reliability.

Installation

npm install big-schema

Quick Start

Import

import { str, num, bool, obj, arr, date, union, validate } from "big-schema";

Validate Data

const userSchema = obj({
  name: str({ required: true }),
  age: num({ min: 0 }),
  isAdmin: bool(),
});

const result = validate(userSchema, {
  name: "John",
  age: 30,
  isAdmin: true
});

if (result.ok) {
  console.log("Valid data: ", result.value);
} else {
  console.log("Validation errors: ", result.errors);
}

Schema Types and Properties

StringSchema

  • required: Indicates if the field is required.
  • min: Minimum length of the string.
  • max: Maximum length of the string.
  • length: Exact length of the string.
  • pattern: Regular expression the string must match.
  • email: Indicates the string should be a valid email.
  • uri: Indicates the string should be a valid URI.
  • trim: Indicates that the string should be trimmed.
  • lowercase: Indicates the string should be in lowercase.
  • uppercase: Indicates the string should be in uppercase.

NumberSchema

  • required: Indicates if the field is required.
  • min: Minimum value of the number.
  • max: Maximum value of the number.
  • positive: Indicates the number should be positive.
  • negative: Indicates the number should be negative.
  • integer: Indicates the number should be an integer.
  • greater: Specifies that the number should be greater than this value.
  • less: Specifies that the number should be less than this value.

BooleanSchema

  • required: Indicates if the field is required.
  • oneOf: Specifies a whitelist of boolean values.

ArraySchema

  • required: Indicates if the field is required.
  • min: Minimum number of items in the array.
  • max: Maximum number of items in the array.
  • length: Exact number of items in the array.
  • unique: Indicates that all items in the array should be unique.
  • of: Schema for validating items within the array.

ObjectSchema

  • required: Indicates if the field is required.
  • keys: Schema for validating keys within the object.
  • unknown: Allows properties not defined in keys.

DateSchema

  • required: Indicates if the field is required.
  • min: Minimum date value.
  • max: Maximum date value.
  • greater: Specifies that the date should be greater than this value.
  • less: Specifies that the date should be less than this value.

UnionSchema

  • of: An array of schemas for validating the union.

Advanced Examples

Nested Object and Array

const productSchema = obj({
  id: num({ required: true }),
  name: str({ required: true, min: 1 }),
  tags: arr(str({ min: 1 }), { min: 1 }),
  manufacturer: obj({
    name: str({ required: true }),
    location: str(),
  }),
});

const productData = {
  id: 1,
  name: "Laptop",
  tags: ["Electronics", "Portable"],
  manufacturer: {
    name: "TechCorp",
    location: "USA",
  },
};

const result = validate(productSchema, productData);

Union Schema

const petSchema = union([
  obj({
    type: str({ required: true, pattern: /^Dog$/ }),
    breed: str({ required: true }),
  }),
  obj({
    type: str({ required: true, pattern: /^Cat$/ }),
    livesLeft: num({ min: 0, max: 9 }),
  }),
]);

const petData = {
  type: "Cat",
  livesLeft: 7,
};

const result = validate(petSchema, petData);

Contribution

If you find any bugs or have a feature request, please open an issue. Contributions are welcome.