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 - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

9

package.json
{
"name": "tiva",
"version": "0.1.3",
"version": "0.1.4",
"description": "Expensive plain object type validator leverages TypeScript language service.",

@@ -35,4 +35,8 @@ "license": "MIT",

"prettier": "^1.19.0",
"ts-jest": "^24.1.0"
"ts-jest": "^24.1.0",
"typescript": "^3.8.3"
},
"peerDependencies": {
"typescript": ">=3.8.0"
},
"dependencies": {

@@ -42,5 +46,4 @@ "tslang": "^0.1.18",

"tsutils": "^3.17.1",
"typescript": "3.8.0-dev.20191105",
"validator": "^12.0.0"
}
}

@@ -9,2 +9,14 @@ [![NPM Package](https://badge.fury.io/js/tiva.svg)](https://www.npmjs.com/package/tiva)

## Why
| Category | Complex Type | Extended Validation | Zero Build Setups | Performance |
| ----------------------------------------------- | ------------ | ------------------- | ----------------- | -------------------------- |
| Tiva | Yes | Yes | Yes | In many case not tolerable |
| [ajv](https://github.com/epoberezkin/ajv) alike | Yes | Yes\* | No | Much much better than Tiva |
| [io-ts](https://github.com/gcanti/io-ts) alike | Limited\*\* | Yes | Yes | Much 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

@@ -33,3 +45,3 @@

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

@@ -51,6 +63,34 @@ ```ts

Custom extensions can be provided by `options.extensions` of `Tiva` constructor.
Writing custom extensions is easy:
```ts
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.
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