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

@typeonly/validator

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typeonly/validator

An API to validate JSON data or JavaScript objects, using TypeScript typing definitions.

  • 0.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
393
increased by38.87%
Maintainers
1
Weekly downloads
 
Created
Source

@typeonly/validator

Build Status npm Type definitions GitHub

TypeOnly aims to be the pure typing part of TypeScript. This package provides an API to validate JSON and JavaScript objects with TypeScript definitions, using the TypeOnly parser.

Tutorial: How to programmatically validate JSON data

At first, it is necessary to follow the tutorial of TypeOnly in order to generate a .to.json file based on your TypeScript definitions. After this step, you have the following file: dist/types.to.json.

Now, add @typeonly/validator to the project:

npm install @typeonly/validator

Create a file src/validate-main.js with the following content:

// src/validate-main.js
const { createValidator } = require("@typeonly/validator");

const data = {
  color: "green",
  shape: {
    kind: "circle",
    x: 100,
    y: 100,
    radius: 50
  }
};

const validator = createValidator({
  bundle: require("./types.to.json")
});
const result = validator.validate("./drawing", "Drawing", data);
console.log(result);

This code validates the data object using RTO files generated by the TypeOnly parser.

Execute it:

$ node src/validate-main.js
{ valid: true }

Contribute

With VS Code, our recommanded plugin is:

  • TSLint from Microsoft (ms-vscode.vscode-typescript-tslint-plugin)

Keywords

FAQs

Package last updated on 14 Jun 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