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

type-standard-validator

v1.1.4

Published

A validator package with infered types to help with development and object validation.

Downloads

17

Readme

Type Standard Validator (TSV)

npm version License

This is a npm package attempting to replace popular validation libraries like Joi and Yup offering a better type expereince from them. The main focus of this project is to return a correctly typed value back after validation so that intelisense for JavaScript and types for TypeScript work correctly.

Installation

You can install this package using npm or yarn:

npm install type-standard-validator
# or
yarn add type-standard-validator

Usage

const tsv = require("type-standard-validator");
// this input might be the data from http request or
// dynamic data that you don't know the type of and need to
// verify / validate the structure!
const exampleInput = {
  foo: "example",
  bar: 1,
};

const objectSchema = tsv.object({
  foo: tsv.string(),
  bar: tsv.number().optional(),
});

try {
  // Example usage
  const result = objectSchema.validate(exampleInput);

  // result should have proper types and everything!
  // intelisense should work.
  // ...
} catch (err) {
  // if validation fails a ValidationError is thrown
  if (err instanceof ValidationError) {
    // do something or display error
    //...
  }
}

API Reference

tsv.number()

Create a number validator

Returns:

  • (NumberValidator): A validator with some extra functions to customize

Example:

const tsv = require("type-standard-validator");
const schema = tsv.number();
const numberValue = schema.validate(1); // the input here
console.log(numberValue); // 1
const numberValue2 = schema.validate("2");
console.log(numberValue2); // 2 casts valid number strings to number
const notNumber = schema.validate("example"); // throws ValidationError

tsv.string()

Create a string validator

Returns:

  • (StringValidator): A validator with some extra functions to customize

Example:

const tsv = require("type-standard-validator");
const schema = tsv.string();
const stringValue = schema.validate("test"); // the input here
console.log(stringValue); // test
const errorValue = schema.validate(2); // throws ValidationError

tsv.array(itemsValidator)

Create an array validator

Parameters:

  • itemsValidator (Validator): Any valid Validator describing the items within the array;

Returns:

  • (ArrayValidator): A validator with some extra functions to customize

Example:

const tsv = require("type-standard-validator");
const schema = tsv.array(tsv.number());
const arrayValue = schema.validate([]); // the input here
console.log(arrayValue); // []
const arrayValue2 = schema.validate([1, "2", 3]); // the input here
console.log(arrayValue2); // [1, 2, 3] - remember number validator can cast valid number strings to numbers
const errorValue = schema.validate([1, "test"]); // throws ValidationError

tsv.object(structure)

Create an object validator

Parameters:

  • structure ({ [key]: Valitator }): An object with the expected keys and for each key a valid validator

Returns:

  • (ObjectValidator): A validator with some extra functions to customize

Example:

const tsv = require("type-standard-validator");
const objectSchema = tsv.object({
  foo: tsv.string(),
  bar: tsv.number().optional(),
});

const result = objectSchema.validate({
  foo: "test",
});
console.log(result); // { foo: "test" }
const result2 = objectSchema.validate({
  foo: "test",
  bar: 1,
});
console.log(result2); // { foo: "test", bar: 1 }
const result3 = objectSchema.validate({
  foo: "test",
  bar: "wrong",
}); // throws ValidationError

Contributions

Please create a changeset file for your changes before you sumbit a pull request.

  npx changeset