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

@kitmi/validators

v1.4.1

Published

Dynamic validators to be used with @kitmi/types based on @kitmi/jsonv and @kitmi/jsonx

Downloads

236

Readme

@kitmi/validators

Dynamic Validator

@kitmi/validators is a comprehensive validation library designed to validate objects using a declarative syntax known as Js Type Modifiers Syntax (JTMS). It allows for dynamic validation strategies by using various types of modifiers that can be combined to form complex validation rules.

Installation

To install @kitmi/validators, please use one of the following package managers:

Using Bun:

bun install @kitmi/validators

Using npm:

npm install @kitmi/validators

Modifier Syntax

Modifiers in JTMS can be expressed in different formats, depending on whether they require arguments:

  • Standalone Modifiers: These are simply strings that consist of a modifier prefix followed by the modifier's name.
  • Modifiers with Arguments: These can be expressed either as objects or arrays:
    • Object Style:
      • name: Modifier name (e.g., ~mobile)
      • options: Arguments for the modifier (e.g., { locale: 'en-US' })
    • Array Style:
      • Index 0: Modifier
      • Index 1: Modifier options argument

Types of Modifiers

Modifiers are categorized based on their prefix:

  • Validator (~): Validates the value.
  • Processor (>): Transforms the value.
  • Activator (=): Provides a default value if the current value is null.

Modifier Handlers

Each type of modifier utilizes a different handler function with a specific signature:

  • Validator Handler: (value, options, meta, context) => [true/false, null/failed reason]

    • A validator that returns false will halt the modifier pipeline and raise a ValidationError.
  • Processor Handler: (value, options, meta, context) => transformedValue

  • Activator Handler: (options, meta, context) => defaultValue

    • An activator is invoked only if the current value is null.

Sample Usage

Here is an example demonstrating how to use @kitmi/validators:

import validator from '@kitmi/validators';

const result = validator.sanitize(obj, {
    type: 'object',
    schema: {
        key1: {
            type: 'integer',
            post: [
                ['~max', 30],
                ['~min', 10],
            ],
        },
        key2: {
            type: 'integer',
            post: [
                ['~max', 20],
                ['~min', 10],
            ],
        },
    },
    optional: true,
    post: [
        {
            name: '~jsv',
            options: {
                key1: {
                    $gt: '$$.key2',
                },
            },
        },
        [
            '>jsx',
            {
                $toArray: { name: '$$KEY', value: '$$CURRENT' },
            },
        ],
        '=default',
    ],
});

console.log(result);
// Output: [{ name: 'key1', value: 20 }, { name: 'key2', value: 15 }]

Synchronous and Asynchronous Usage

Synchronous Mode

import validator, { Types } from '@kitmi/validators';

validator.addValidator('isEmail', () => [true / false, 'reason if false']);
validator.addProcessor('escape', () => {});
validator.addActivator('randomFill', () => {});
const sanitizedValue = validator.sanitize(obj, schema);

Asynchronous Mode

import validator, { Types } from '@kitmi/validators/async';

validator.addValidator('isEmail', async () => [true / false, 'reason if false']);
validator.addProcessor('escape', async () => {});
validator.addActivator('randomFill', async () => {});
const sanitizedValue = await validator.sanitize_(obj, schema);

Including All Modifiers

For convenience, you can import versions of the library that include all built-in modifiers:

  • Synchronous: import validator from '@kitmi/validators/allSync';
  • Asynchronous: import validator from '@kitmi/validators/allAsync';

License

This project is licensed under the MIT License.

Copyright (c) 2023 KITMI PTY LTD

Written by GPT-4