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

@taikai/validator

v1.1.4

Published

Validator library to check data-model

Downloads

10

Readme

Validator

A simple validator library to check data-model from the client, it works with ts as well with js projects.

import Validator from '@taikai/validator';

const validator = new Validator({});
const isValid1 = await validator.validate({
    name: {
        type: 'string',
        options: {
            presence: true
        }
    }
}, {
    name: 'John Doe'
});

:memo: Where do I start?

Step 1: Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. prefer the LTS version.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

$ npm i @taikai/validator

Step 2: Validate a model with the validator

Import the library into your project

use the import statement (in case of a typescript project):

import Validator from '@taikai/validator';

or require (if you are working in a js project):

const { default: Validator } = require('@taikai/validator');

Create a new instance of your Validator

the validator instance expects a context object as the first parameter. the context is na object containing a db property (as PrismaClient instance object) to achieve the database if need at some point.

// Example
const validator = new Validator({
    // Use only if SchemaOptions `unique` property is defined
    db: new PrismaClient()
});

the db is required only when the SchemaOptions unique combined with orm properties are defined when validating a model.

//... Instance with valid context ... 

await validator.validate({
    username: {
        type: 'string',
        options: {
            presence: true,
            unique: true, // required
            orm: 'user' // required
        }
    }
}, {
    username: 'joshmathias'
}); // true

// In case of errors, we can find in the `errors` property of the 
// validator instance
console.log(validator.errors); // {}

Reusing a validator instance

we can re-use a validator instance by calling the reset() method:

const validator = new Validator({});

// #1 Validation
await validator.validate({
    username: {
        type: 'string',
        options: {
            presence: true,
        }
    }
}, {}); // false

console.log(validator.errors); // { 'username': 'name is not a valid string' }

// Reseting...
validator.reset();

// #2 Validation
await validator.validate({
    name: {
        type: 'string',
        options: {
            presence: true,
        }
    }
}, {}); // false

console.log(validator.errors); // { 'name': 'name is not a valid string' }
Note: When you call the `reset()` method it's clears the errors object.

Contributing

The way you can contribute to this project is submitting new features or fixing bugs.

To get started you have to clone this repo to your machine; open your terminal on the folder you want to clone into, and enter the following commands:

$ git clone https://github.com/taikai/validator.git
$ cd validator
$ npm install

Now you can create a new branch containing the new feature or bugfix, e.g.: git checkout -b feature/my_new_feature. This will make it easier for you to submit a pull request and get your feature merged.

Test Options

Before submitting, you must pass all the unit tests and syntax checks by running the two commands below:

npm run test run all the unit test cases in tests folder

There is a nested project called lib-tester where you can play and see how would work your new feature or bugfix. But first you need to run the build script on the root project, then install the dependencies on the nested project using the npm install command. It should install the local package NOT the published one:

{
    // ...
    "@taikai/validator": "file:../taikai-validator-<VERSION>.tgz"
    // ...
}