Comments (2)
@jcmoore Hi!
At this point in time, AOT compilation will require you to embed 3 auxiliary function calls into the output (if you're using custom types and formats). TypeBox will emit calls to these functions to perform exterior value checks. The following provides some high level information on the current design.
Custom Type AOT Compilation
The following functions will need to be embedded into the compiled string output.
kind(kind, ordinal, value)
- check kind against valueformat(format, value)
- check format against string valuehash(value)
- return value or unique hash of a value (if object)
Here is a quick draft setting up these functions (it's not very elegant)
import { Type, Kind, TypeRegistry, FormatRegistry, TSchema } from '@sinclair/typebox'
import { TypeCompiler } from '@sinclair/typebox/compiler'
// need to register these types for the compiler to pass preflight checks
TypeRegistry.Set('DataView', (schema, value) => value instanceof DataView)
FormatRegistry.Set('Email', (value) => value === '[email protected]')
// User defined AOT compile function
function CompileAOT<T extends TSchema>(name: string, schema: T) {
// These functions need to be embedded into the generated out.
const intrinsicFunctions = (`
function format(format: string, value: string) {
if(format === 'Email') return value === '[email protected]'
return false
}
function kind(kind: string, ordinal: number, value: unknown) {
if(kind === 'DataView') return value instanceof DataView
return false
}
function hash(value: unknown) {
return value
}
`)
// Compile with TypeScript annotations
const checkFunction = TypeCompiler.Code(T, { language: 'typescript' })
// Wrap everything in IIFE
return `const ${name} = (() => { ${[intrinsicFunctions, checkFunction].join('\n')}})()`
}
// ---
const T = Type.Object({
dataview: Type.Unsafe({ [Kind]: 'DataView' }),
email: Type.String({ format: 'Email' }),
unique: Type.Array(Type.Number(), { uniqueItems: true })
})
const C = CompileAOT('check', T)
console.log(C)
// const check = (() => {
// function format(format: string, value: string) {
// if(format === 'Email') return value === '[email protected]'
// return false
// }
// function kind(kind: string, ordinal: number, value: unknown) {
// if(kind === 'DataView') return value instanceof DataView
// return false
// }
// function hash(value: unknown) {
// return value
// }
// return function check(value: any): boolean {
// return (
// (typeof value === 'object' && value !== null && !Array.isArray(value)) &&
// kind('DataView', 0, value.dataview) &&
// (typeof value.email === 'string') &&
// format('Email', value.email) &&
// Array.isArray(value.unique) &&
// value.unique.every((value: any) => (Number.isFinite(value))) &&
// ((value: any) => { const set = new Set(); for(const element of value) {
// const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) }
// } return true } )(value.unique)
// )
// }})()
Unfortunately, the above isn't very elegant as it does require the implementer to perform some fairly arcane string manipulation, but it is the best implementation under the current design.
Am I misunderstanding something about how AOT compilation is intended to work? Would you consider extending the TypeCompiler (or TypeRegistry) API with something like the following (probably to be used in the TypeCompiler.Visit() method)?
The above is generally how AOT compilation is intended to work where TypeBox pushes the complexity of generating AOT compiled output to the implementer (where AOT is usually integrated into build tools). The design isn't ideal tbh and there is room to improve things, but there is a bit of indecision on the best way to improve things... I think if implementing the SetGenerator, GetGenerator idea, I think this would just be a case of updating the TypeRegistry to support returning string check functions, something like the following.
TypeRegistry('DataView',
(schema, value) => value instanceof DataView,
(schema) => `value instanceof DataView` // added
)
However I'm a bit reluctant to take on any updates here at the moment as I'm currently on the look out for better approaches to creating custom types in TypeBox...ideally approaches that wouldn't require the TypeRegistry at all (as I'd like to phase out this registry over the next few revisions)
The main thing holding back a more elegant AOT is mostly limitations in Json Schema where there isn't a way to appropriately validate against general JavaScript structures. TypeBox already provides a couple of extension types (Uint8Array and Date), but where these could also be phased out in favor of the following...
const DataView = Type.Unsafe<DataView>(Type.InstanceOf('DataView'))
// const DataView = { instanceof: 'DataView' }
// would generate -> value instanceof globalThis['DataView']
As such, I'm currently considering putting together a "JavaScript Schema" specification that extends "Json Schema" with additional keywords that enable it to validate against arbitrary JavaScript structures. This with the specific goal of encoding enough information in the schematics such that both Value.* checks and AOT generation can be derived from the schematics only (and not require registration)
This is a bit of long and winding response (sorry!) but hope it provides some insights into TypeBox AOT and where I'd like to take things in the library moving forward.
Happy to continue this discussion.
Cheers
S
from typebox.
@jcmoore Heya,
Might convert this issue into a discussion as the example above is the current design (warts and all). As mentioned, I think there is room is improve AOT, but I think the path towards that involves devising extended schematics (keywords) that can appropriately validate JavaScript objects (this rather than adding more functions to the API surface).
Again, happy to continue a discussion on this.
Cheers
S
from typebox.
Related Issues (20)
- Default values in nested objects are not generated HOT 2
- Doubt on Record HOT 2
- TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema HOT 2
- Max call stack size exceeded HOT 1
- Value.Convert() doesn't work with nullable record HOT 1
- Variadic Any Typed function parameters HOT 2
- How to do a cross field validation? HOT 3
- Undefined is incorrectly handled in Diff HOT 3
- QoL helper: `TypeCheck.Ensure` HOT 3
- Usage of `Type.Composite` with `Type.TemplateLiteral` HOT 3
- `Check` doesn't validate strings with enums HOT 4
- `Type.Omit` mixed with `Type.Union` is not preserving the `additionalProperties` attribute HOT 2
- Advice to compose types HOT 3
- Access to schema properties for non TObject schemas HOT 5
- Type.Optional with Type.Transform unexpected behaviour HOT 1
- ts(7056) when you try to export a big union type HOT 1
- `Value.Default` does not traverse object schema HOT 2
- Unable to use a union of literal as record key type after upgrading version HOT 2
- Unknown format 'email' after update HOT 3
- Uncaught Error: Cannot access 'Object' before initialization HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.