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

validation.ts

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validation.ts

Validation for TypeScript

  • 0.0.2
  • npm
  • Socket score

Version published
Weekly downloads
749
increased by5.05%
Maintainers
1
Weekly downloads
 
Created
Source

validation.ts

Validation for TypeScript

validate

Every validator has a validate function which returns a Result

A validated value can be transformed at any point during the validation process (e.g. isoDate).
Errors are accumulated.

import { errorDebugString } from 'validation.ts'

const myValidator = ...

const result = myValidator.validate(myJson)

result.fold(
  errors => console.error(errorDebugString(errors)),
  validatedJson => console.log(validatedJson)
)

primitives

import * as v from 'validation.ts'

v.string
v.number
v.boolean
v.null
v.undefined
v.isoDate

literal

import { literal } from 'validation.ts'

// The only value that can ever pass this validation is the 'X' string literal
const validator = literal('X')

array

import { array, string } from 'validation.ts'

const validator = array(string)

object, literal union, optional

import { string, object, union, optional } from 'validation.ts'


const person = object({
  id: string,
  prefs: object({
    csvSeparator: optional(union(',', ';', '|'))
  })
})

Note: For bigger unions, consider using the keyof validator instead.

dictionary

A dictionary is an object where all keys and all values share a common type.

import { dictionary, string, number } from 'validation.ts'

const validator = dictionary(string, number)

keyof

import Set from 'space-lift/object/set'
import { keyof } from 'validation.ts'

const keys = Set('aa', 'bb', 'cc').value()

const keyValidator = keyof(keys)

keyValidator.validate('bb') // Ok<'aa' | 'bb' | 'cc'> = Ok('bb')

// keyof typeof keys === typeof keyValidator.T === 'aa' | 'bb' | 'cc'

map, filter

import { map, filter, string } from 'validation.ts'

const validator = string.filter(str => str.length > 3).map(str => `${str}...`)

recursion

import { recursion, string, array, object }

type Category = { name: string, categories: Category[] }

const category = recursion<Category>(self => object({
  name: string,
  categories: array(self)
}))

Deriving the typescript type from the validator type

Note: this can be used with any combination of validators except ones using recursion.

import { object, string, number } from 'validation.ts'

const person = object({
  name: string,
  age: number
})

type Person = typeof person.T
/*
type Person = {
  name: string
  age: number
}
*/

Thanks

To gcanti and his io-ts library which provided great inspiration.

Keywords

FAQs

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