You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

arktype

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arktype

TypeScript's 1:1 validator, optimized from editor to runtime

1.0.26-alpha
Source
npmnpm
Version published
Weekly downloads
290K
3.2%
Maintainers
1
Weekly downloads
 
Created

What is arktype?

The arktype npm package is a powerful tool for defining and validating complex data structures in JavaScript and TypeScript. It allows developers to create type-safe schemas and perform runtime validation, ensuring that data conforms to expected formats.

What are arktype's main functionalities?

Type Definition

Arktype allows you to define types for your data structures. In this example, a user type is defined with specific fields and their types. The `userType` function can then be used to validate objects against this schema.

const { type } = require('arktype');

const userType = type({
  name: 'string',
  age: 'number',
  email: 'string'
});

const user = {
  name: 'John Doe',
  age: 30,
  email: 'john.doe@example.com'
};

console.log(userType(user)); // true if valid, otherwise throws an error

Nested Structures

Arktype supports nested structures, allowing you to define complex data types with nested objects. This example demonstrates how to define a user type that includes an address object.

const { type } = require('arktype');

const addressType = type({
  street: 'string',
  city: 'string',
  zip: 'string'
});

const userType = type({
  name: 'string',
  age: 'number',
  address: addressType
});

const user = {
  name: 'Jane Doe',
  age: 25,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    zip: '12345'
  }
};

console.log(userType(user)); // true if valid, otherwise throws an error

Custom Validation

Arktype allows for custom validation logic using type expressions. In this example, a type is defined for positive numbers, and a function is created to validate numbers against this type.

const { type } = require('arktype');

const positiveNumberType = type('number & >0');

const validatePositiveNumber = (num) => {
  try {
    positiveNumberType(num);
    return true;
  } catch (e) {
    return false;
  }
};

console.log(validatePositiveNumber(5)); // true
console.log(validatePositiveNumber(-3)); // false

Other packages similar to arktype

FAQs

Package last updated on 31 Oct 2023

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