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

runtypes

Package Overview
Dependencies
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

runtypes

Runtime validation for static types

  • 0.10.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
245K
increased by10.02%
Maintainers
1
Weekly downloads
 
Created

What is runtypes?

Runtypes is a TypeScript library that provides runtime validation and type checking for JavaScript objects. It allows developers to define types and validate data against those types at runtime, ensuring that the data conforms to the expected structure and types.

What are runtypes's main functionalities?

Basic Type Validation

Runtypes allows you to define basic types like String and Number and validate data against these types. If the data does not match the expected type, an error is thrown.

const { String, Number } = require('runtypes');

const Name = String;
const Age = Number;

Name.check('John Doe'); // Passes
Age.check(30); // Passes

// Name.check(123); // Throws an error
// Age.check('30'); // Throws an error

Object Validation

Runtypes allows you to define complex types like objects and validate data against these types. You can specify the structure of the object and the types of its properties.

const { Record, String, Number } = require('runtypes');

const Person = Record({
  name: String,
  age: Number
});

Person.check({ name: 'John Doe', age: 30 }); // Passes

// Person.check({ name: 'John Doe', age: '30' }); // Throws an error

Union Types

Runtypes supports union types, allowing you to define a type that can be one of several types. This is useful for validating data that can have multiple valid types.

const { Union, String, Number } = require('runtypes');

const StringOrNumber = Union(String, Number);

StringOrNumber.check('Hello'); // Passes
StringOrNumber.check(123); // Passes

// StringOrNumber.check(true); // Throws an error

Array Validation

Runtypes allows you to define array types and validate data against these types. You can specify the type of the elements in the array.

const { Array, String } = require('runtypes');

const StringArray = Array(String);

StringArray.check(['Hello', 'World']); // Passes

// StringArray.check(['Hello', 123]); // Throws an error

Optional Properties

Runtypes allows you to define optional properties in objects. This is useful for validating data where some properties may or may not be present.

const { Record, String, Number, Optional } = require('runtypes');

const Person = Record({
  name: String,
  age: Optional(Number)
});

Person.check({ name: 'John Doe' }); // Passes
Person.check({ name: 'John Doe', age: 30 }); // Passes

// Person.check({ name: 'John Doe', age: '30' }); // Throws an error

Other packages similar to runtypes

FAQs

Package last updated on 05 Apr 2017

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