Skip to content

Quick Start

Install the package:

sh
$ npm add pure-parse
sh
$ pnpm add pure-parse
sh
$ yarn add pure-parse
sh
$ yarn add pure-parse
sh
$ bun add pure-parse

Create a parser by composing higher-order functions with parser primitives:

ts
import { object, parseString, parseNumber } from 'pure-parse'

const parseUser = object({
  name: parseString,
  age: parseNumber,
})
ts
import { objectGuard, isString, isNumber } from 'pure-parse'

const isUser = objectGuard({
  name: isString,
  age: isNumber,
})

TIP

You can create type guards with a similar syntax: use the tabs to switch between examples for parsers and type guards.

Declare the Type

Define a type alias to type-check the parser:

ts
import { object, parseString, parseNumber } from 'pure-parse'

type User = {
  name: string
  age: number
}

const parseUser = object<User>({
  name: parseString,
  age: parseNumber,
})
ts
import { objectGuard, isString, isNumber } from 'pure-parse'

type User = {
  name: string
  age: number
}

const isUser = objectGuard<User>({
  name: isString,
  age: isNumber,
})

If the parser does not match the type argument (User), you will get a type error. This powerful feature ensures that the parsed result value always adheres to the type parameter.

...or Infer the Type

You can also infer the type from the parser:

ts
import { object, parseString, parseNumber, Infer } from 'pure-parse'

const parseUser = object({
  name: parseString,
  age: parseNumber,
})

type User = Infer<typeof parseUser>
ts
import { objectGuard, isString, isNumber, Infer } from 'pure-parse'

const isUser = objectGuard({
  name: isString,
  age: isNumber,
})

type User = Infer<typeof isUser>

This lets you write less repeated code, but the drawback is that your types are coupled to the library.

Validate Data

The result from parsing is a tagged union with the property tag as discriminator:

ts
import { formatResult, object, parseString, parseNumber } from 'pure-parse'

const parseUser = object({
  name: parseString,
  age: parseNumber,
})

// Replace `data` with your own input
const result = parseUser(data)

if (result.error) {
  console.log(formatResult(result))
  return
}

// Success!
console.log(`The user's name is "${result.value.name}"`)
ts
import { objectGuard, isString, isNumber } from 'pure-parse'

const isUser = objectGuard({
  name: isString,
  age: isNumber,
})

// Replace `data` with your own input
if (!isUser(data)) {
  console.log('The data is not a user')
  return
}

// Success!
console.log(`The user's name is "${data.name}"`)