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

checkmate-ts

v1.0.9

Published

Checkmate TypeScript - A powerful and flexible object validator written in TypeScript, supporting validation for various data types, nested objects, and arrays. Ensures type safety and reliability in data validation scenarios.

Downloads

349

Readme

Checkmate

checkmate-ts is a library for validating objects in TypeScript applications. It supports validating various data types, nested objects, arrays, and custom rules, ensuring type safety and reliability in data validation scenarios.

Types will be inferred from the validation rules, but might not work reliably (yet).

Usage

import {Validator} from 'checkmate-ts'

const {user, title, topics} = Validator.create({
  'title': ['string', 'required', 'min:8'],
  'topics': ['array', 'min:1'],
  'topics.*': ['string', 'required'],
  'user': ['required'],
  'user.id': ['required', 'number'],
  'user.name': ['required', 'string'],
  'user.role': ['required', 'in:admin,moderator']
}).validateObject<{
  title: string,
  topics: string[],
  user: {
    id: number,
    name: string,
    role: 'admin' | 'moderator'
  }
}>(res)

Important information

Nested arrays currently only work with one level.

Installation

You can install the package via npm:

npm install checkmate-ts

Or via yarn:

yarn add checkmate-ts

Rules

The validator supports the following rules:

required: The field must be present.

string: The field must be a string.

number: The field must be a number.

array: The field must be an array.

min<number>: The field must be a string with a minimum length of .

max:<number>: The field must be a string with a maximum length of .

email: The field must be a valid email address.

in:<values>: The field value must be one of the specified .

Methods

Create

create<R extends Rules>(rules: R): Validator<R>

Creates a new instance of the Validator class with specified validation rules.

parameters: rules - The rules to match against returns: validator - An instance of the validator

validateObject

validateObject<T>(data: T): T Validates the provided data object against the rules defined in the validator instance.

parameters: data - The object to be validated returns: data - The data object, it doesn't mutate this object in any way.

Example


import {ValidationError, Validator} from 'checkmate-ts'

// Define validator with rules
const validator = Validator.create({
  // Simple fields
  'name': ['required', 'string', 'min:3', 'max:50'],
  'age': ['required', 'number'],

  // Nested object
  'address.street': ['string'],
  'address.city': ['required', 'string'],

  // Array of strings
  'skills': ['array'],
  'skills.*': ['string'],

  'email': ['required', 'email'],

  // Must be either "active" or "inactive"
  'status': ['in:active,inactive'],
})

const data = {
  name: 'John Doe',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown'
  },
  skills: ['JavaScript', 'TypeScript'],
  email: '[email protected]',
  status: 'active'
}

try {
  // Validate the data object
  const validatedData = validator.validateObject<{
    name: string,
    age: number,
    address: {
      street?: string,
      city: string
    },
    skills: string[],
    email: string,
    status: 'active' | 'inactive'
  }>(data)

} catch (error) {
  if (error instanceof ValidationError) {
    console.log(error.errors)
  }
}