Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiva

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiva

Expensive plain object type validator leverages TypeScript language service.

  • 0.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.7K
increased by257.55%
Maintainers
1
Weekly downloads
 
Created
Source

NPM Package Build Status Coverage Status

Tiva

⏱️ EXPENSIVE plain object type validator leverages TypeScript language service.

Why

CategoryComplex TypeExtended ValidationZero Build SetupsPerformance
TivaYesYesYesIn many case not tolerable
ajv alikeYesYes*NoMuch much better than Tiva
io-ts alikeLimited**YesYesMuch much better than Tiva

* Extra efforts might be needed for extended validation to work with TypeScript declarations.

** We have many types that are evaluated from pure type declarations, and those type declarations are used in other part of our project for type checking and intellisense purpose. And aside from that, writing complex type with those tools will not be as pleasant as natural type declarations.

Usage

yarn add tiva
import {Tiva} from 'tiva';

let tiva = new Tiva();

tiva.validate('string[]', ['foo', 'bar']).then(console.info, console.error);

tiva
  .validate(
    {module: 'module-specifier', type: 'AwesomeType'},
    {foo: 'abc', bar: 123},
  )
  .then(console.info, console.error);

Extensions

Tiva can validate with extended validator functions that matches by @tag in JSDoc comments (one tag per line):

interface Foo {
  /** @uuid */
  id: string;
}

There are a few built-in extensions:

  • @pattern <pattern> Validate by regular expression pattern.
  • @uuid [version] UUID.
  • @unique [group] Validate that there's no more than one occurrence.

Checkout @built-in-extensions.ts for implementation details.

Writing custom extensions is easy:

let tiva = new Tiva({
  extensions: {
    custom(value) {
      if (value === 'custom') {
        return undefined;
      }

      return `Value "${value}" must be "custom"`;
    },
  },
});

How it works

Tiva provides a Validator class that synchronously manipulates TypeScript language service to do the heavy lifting; and a Tiva class that creates a worker to run Validator in another thread.

The type check part is simple: it just gets the diagnostic messages from TypeScript by fabricating a variable statement. The tricky part is the extended validation.

Here's how Tiva does it:

  1. It recursively visits the types used by the type to be validate against.
  2. It find tags like @uuid in those types, and asks TypeScript to find the implementations within the plain object to be validated.
  3. It then validates the value against the extension.

Again the heavy lifting is done by the TypeScript language service. And doing this way also makes it possible to have Tiva work with complex types including condition types, mapping types etc.

License

MIT License.

FAQs

Package last updated on 29 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc