TS Prove

A composable typesafe library for decoding ambiguas data and validating structured types.

GitHub Workflow Status Coverage Status Dev Dependencies styled with prettier

installing

npm install ts-prove
yarn add ts-prove

Api

The libary provides a list of curried composable validation functions.

p: { string, number, null, undefined, any, unknown, oneOf, array, shape }

const validateString = p.string()
validateString('Hello World') // [null, 'Hello World'"]

These can be composed into more complex structures ie for validating client requests on a server.

const createPersonPayload = p.shape({
    name: p.string()
    friends: p.array(p.oneOf(
        p.string(),
        p.shape({ id: p.string() })
    ))
})

const handler = (event, context) => {
    const [error, payload] = createPersonPayload(JSON.parse(event.body))

    // Returns structured errors
    // ['{ name: "John", friends: [ ([10] { id: __missing__ }) ] }', unknown]
    if (error !== null) return { statusCode: 500, body error}

    // And fully typed payloads
    // payload: [null, { name: string, friends: (string | { id: string })[] } ]
}

This project follows the all-contributors specification. Contributions of any kind are welcome!