You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example is a reference implementation of [Standard Schema](https://github.com/standard-schema/standard-schema) for TypeBox.
4
+
5
+
### Overview
6
+
7
+
This example provides a reference implementation for the Standard Schema specification. Despite the name, this specification is NOT focused on schematics. Rather, it defines a set of common TypeScript interfaces that libraries are expected to implement to be considered "Standard" for framework integration, as defined by the specification's authors.
8
+
9
+
The TypeBox project has some concerns about the Standard Schema specification, particularly regarding its avoidance to separate concerns between schematics and the logic used to validate those schematics. TypeBox does respect such separation for direct interoperability with industry standard validators as well as to allow intermediate processing of types (Compile). Additionally, augmenting schematics in the way proposed by Standard Schema would render Json Schema invalidated (which is a general concern for interoperability with Json Schema validation infrastructure, such as Ajv).
10
+
11
+
The Standard Schema specification is currently in its RFC stage. TypeBox advocates for renaming the specification to better reflect its purpose, such as "Common Interface for Type Integration." Additionally, the requirement for type libraries to adopt a common interface for integration warrants review. The reference project link provided below demonstrates an alternative approach that would enable integration of all type libraries (including those not immediately compatible with Standard Schema) to be integrated into frameworks (such as tRPC) without modification to a library's core structure.
The Standard Schema function will augment TypeBox's Json Schema with runtime validation methods. These methods are assigned to the sub property `~standard`. Once a type is augmented, it should no longer be considered valid Json Schema.
18
+
19
+
```typescript
20
+
import { Type } from'@sinclair/typebox'
21
+
import { StandardSchema } from'./standard'
22
+
23
+
// The Standard Schema function will augment a TypeBox type with runtime validation
24
+
// logic to validate / parse a value (as mandated by the Standard Schema interfaces).
25
+
// Calling this function will render the json-schema schematics invalidated, specifically
26
+
// the non-standard keyword `~standard` which ideally should be expressed as a non
27
+
// serializable symbol (Ajv strict)
28
+
29
+
const A =StandardSchema(Type.Object({ // const A = {
Applying Standard Schema to a TypeBox types renders them unusable in Ajv. The issue is due to the `~standard` property being un-assignable as a keyword (due to the leading `~`)
47
+
48
+
```typescript
49
+
importAjvfrom'ajv'
50
+
51
+
const ajv =newAjv().addKeyword('~standard') // cannot be defined as keyword.
52
+
53
+
const A =StandardSchema(Type.Object({ // const A = {
0 commit comments