prelude

@prelude/refute · v2.0.1

SourceAPI referenceChangelognpm

Refute module

Usage

npm i -E @prelude/refute
import * as $ from '@prelude/refute'

const refute = $.object({
  foo: $.string,
  bar: $.number
})

const predicate = $.predicate(refute)

const assert = $.assert(refute)

const value = JSON.parse('...')
if (predicate(value)) {
  // value is { foo: string, bar: number }
}

const value_ = assert(value)
// value_ is { foo: string, bar: number }
// throws if not.

console.log($.reason(refute)({ bar: 'a' }))
// Invalid value at key bar, expected number, got a.

Shared core and deliberate differences

Every check is implemented once in @prelude/validation and shared with @prelude/assert and @prelude/predicate; this package renders the structured failure into Fail { status: 'refuted', reason, received }, where reason spells the path (at key a, at index 1, expected number) and received is the innermost failing value. predicate, assert, reason and safeReason interpret a refute in the other modes. Built-in refutes carry their core validator, so containers compose structurally; a hand-written Refute is adapted from its Fail.

The three packages accept exactly the same values (packages/refute/src/matrix.test.ts checks that). Where they describe a failure differently, it is deliberate and kept for compatibility:

License

This package is dedicated to the public domain under CC0 1.0.