Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Types #282

Merged
merged 8 commits into from Nov 22, 2022
Merged

Custom Types #282

merged 8 commits into from Nov 22, 2022

Conversation

sinclairzx81
Copy link
Owner

@sinclairzx81 sinclairzx81 commented Nov 22, 2022

This PR implements custom types for TypeBox. These types allow implementers to create highly specialized validation rules for data that cannot be expressed via JSON schema.

Custom types have been written to integrate with the TypeBox TypeCompiler, Value and Error modules, however custom error reporting is limited to report there was a problem validating the custom type. This may be expanded upon in subsequent revisions.

Example

import { Type, Kind } from '@sinclair/typebox'
import { Custom } from '@sinclair/typebox/custom'

// Ensure the type is registered first
Custom.Set('BigInt', value => typeof value === 'bigint')

// Create a type with the [Kind]: 'BigInt' to match registered type.
const T = Type.Unsafe<bigint>({ [Kind]: 'BigInt' })  // const T = { [Kind]: 'BigInt' }

// Check values as per usual
const R = Value.Check(T, 65536n)                     // const R = true

To implement a custom type. The symbol property [Kind] is required. The string value given to this property is should match the string argument passed to Custom.Set(...).

@sinclairzx81 sinclairzx81 merged commit f33423f into master Nov 22, 2022
@sinclairzx81 sinclairzx81 deleted the custom branch November 22, 2022 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant