Comments (5)
Can you post a reproduction of the type you are using Composite with?
Also maybe you can tell me how to reuse schemas there is a T.Omit and it allows me to delete fields at a high level, but what if I need to delete it at a lower level with nesting?
Composite only operates on the direct properties of the Object(s) being composited. If you need deeper property merging, you will need to handle that manually (or write a function to handle it), as this isn't supported by TypeBox's standard API.
If you can post the Composite causing issues, I may be able to provide some assistance.
from typebox.
@RumyantsevOlegRelabs I see,
It's unfortunately not possible to compose (or compute) raw Json Schema with TypeBox types in this way. Internally, TypeBox handles computed types (like Composite) by introspecting a special Kind
symbol on each type. This symbol is used quickly identify the structure of the type, and apply the appropriate transformations based on that. Because raw Json Schema does not contain the Kind
symbol, it will not compose in the way you expect.
It is possible to augment an existing schema and apply the Kind
symbol, but this something you will need to write yourself. The following augments a simple { x, y, z }
object with the necessarily symbols (making it compatible with Composite). You can also console.log(schema)
to print out the symbols applied to TypeBox generated types as a reference.
import { Type as T, Kind, TObject, TNumber } from '@sinclair/typebox'
// Note: Raw Json Schema is not immediately compatible with computed type composition as
// computed types operate exclusively on a Kind type descriptor. Additionally, computable
// schematics are limited to a finite set of recognized schematics supported by TypeBox.
const SomeOtherStyleSchema = {
[Kind]: 'Object', // <-- required Kind here
type: 'object',
required: ['x', 'y', 'z'],
properties: {
x: { type: 'number', [Kind]: 'Number' }, // <-- required Kind here
y: { type: 'number', [Kind]: 'Number' }, // <-- required Kind here
z: { type: 'number', [Kind]: 'Number' } // <-- required Kind here
}
}
const S = T.Object({
todo: T.String(),
age: T.Number(),
some: SomeOtherStyleSchema as TObject<{ x: TNumber, y: TNumber, z: TNumber }> // <-- requires as TObject
})
Alternative
Instead of using Composite, you can try Intersect. Note that Composite is a VERY advanced type and requires deep introspection into the types it's being asked to compose, however Intersect does not perform any computation on the type, but comes at a cost of expressing the schema with allOf
. Unlike Composite, Intersect does not flatten the schematics into a single output object
schematic, but does produce a schematic that is functionally the same thing (just try replacing Composite for Intersect and see how you go)
Hope this helps
S
from typebox.
@RumyantsevOlegRelabs Hiya,
Might close off this issue as computing types with raw Json Schema is generally not supported. I would recommend trying Intersect over Composite however in your case (as this should compose without computing the flatten schema), however be mindful that you should be using Ajv to validate with raw Json Schema (as with computing types for composition, TypeBox bases it's validation logic of the Kind
symbols as well)
All the best.
S
from typebox.
I can't believe that there isn't just an elementary way to add a field (or series of fields to a schema, since it's a simple function). So I think I'm just doing it wrong and I need to ask for advice
from typebox.
Unfortunately I can't publish the schematic as the project is under NDA. But I can describe it in more detail. Compose is used as in the example and in some places it works correctly. But everything stops working if at least one of the fields of at least one of the compose schemes is not described using a typeBox
Example:
const SomeOtherStyleSchema = {
properties: {
// It's just an example, don't take it seriously
// But I can tell you that this is a valid JSON schema
}
}
T.Object({
todo: T.String(),
age: T.Number(),
some: SomeOtherStyleSchema
})
from typebox.
Related Issues (20)
- JsonTypeBuilder#Record not passing on options HOT 1
- No implicit defaults on Value.Create() HOT 5
- [BUG] Value.create caches the default date HOT 1
- Feature request: Value.Assert and TypeCheck.Assert HOT 3
- [Feature Request?] allow more flexible input for string pattern HOT 3
- Individual types imports naming conflict with native classes HOT 4
- Strict error: Unknown type HOT 2
- Maximum call stack size exceeded. HOT 2
- TUnion of literals from const array HOT 2
- "Value.Clean" doesn't work with Unions of objects with prohibited additionalProperties HOT 3
- JS floating point precision bug causes multipleOf validation to fail HOT 4
- Tuple with rest items (or Array with prefixedItems) HOT 2
- useDefaults HOT 3
- Support for discriminator HOT 1
- Support for self-referencing fields of recursive type HOT 2
- Property '[Kind]' is missing in type 'TObject' but required in type 'TObject<TProperties>' HOT 3
- [feat] Bail / Early Cancel Mode HOT 1
- Setting Description HOT 1
- Schema dependency using Typebox 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.