Replies: 4 comments 1 reply
-
|
@sroussey Hi, There is some new tech in V1 in support of this, Dynamic Validation and Structured Inference. Dynamic ValidationV1 has a (mostly) 2020-12 compliant validator, so it's possible to pass raw JSON Schema to Value.Check(...) or Compile(...) and TypeBox will just treat it as a normal type. You no longer need to convert the schematic to TSchema before using it with validation (as was the case for V0). The only limitation is that dynamically loaded schematics cannot be used with the Type.* compositor (so you can't make a dynamically loaded schema Partial(...) yet), but I will be working on some tools for this in later revisions. Structured InferenceInline with the native JSON Schema validation support, V1 also includes Structured Inference which is able to infer raw JSON Schema. Structured Inference can be useful if loading schematics from remote sources and saving as ReferenceThe following is a quick example Reference import Value from 'typebox/value'
import Type from 'typebox'
// -----------------------------------------------------------
// Dynamic Validation
// -----------------------------------------------------------
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})
// write schema to disk
Deno.writeTextFileSync('./schema.json', JSON.stringify(T))
// read schema from disk
const S = JSON.parse(Deno.readTextFileSync('./schema.json'))
Value.Check(S, { x: 1, y: 2, z: 3 }) // true
Value.Check(S, { x: 1, y: 2, z: 'not-a-number' }) // false
// -----------------------------------------------------------
// Structured Inference
// -----------------------------------------------------------
// can infer if schema structure is observable to TypeScript
type T = Type.Static<{ // type T = {
type: 'object', // x: number;
required: ['x', 'y', 'z'], // y: number;
properties: { // z: number;
x: { type: 'number' }, // }
y: { type: 'number' },
z: { type: 'number' }
}
}>Dynamic Schema (Future)This doesn't exist yet, but being able to load schematics over HTTP, then have loaded schematics computed and mapped by the Type.* compositor is on the roadmap. The main issue is that Type.* only operates on a subset of the JSON Schema specification, but where schematics loaded from elsewhere may contain structures, references, dynamic references, hyperlinks, and other "things" Type.* can't reason about. The main issue is knowing what to do with keywords like Hope this helps |
Beta Was this translation helpful? Give feedback.
-
|
Hi! At this point, I am not worried about arbitrary schemas. Just schemas that I am creating and persisting in a database. No generation of TS required, though some day that might be nice. In fact, my requirements are pretty confined in that it is always a TObject though some properties can be optional and some can be optionally an array (string or string[]). The actual value types are pretty restricted. |
Beta Was this translation helpful? Give feedback.
-
|
For |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
I have a usecase where some things are defined directly in typebox. but other times I have something from an api that i want to convert to typebox. Or I want to use typebox in my code, but need to save a type definition to a database so i need to save out just the json version and then bring it back.
For example:
And I want to send this to another computer via API so I need to just send the JSON and then on the other side I need to take that JSON and make it into a Type.Object.
Beta Was this translation helpful? Give feedback.
All reactions