Comments (3)
@nicholasfinos Hi,
OneOf
I was wondering if there were any plans to work on JSON schema dependencies and/or oneOf in Typebox?
Possibly, but not in the short term. As it stands, Intersect -> AND
, Union -> OR
, and UnionOneOf -> XOR
. The main thing holding back an implementation of OneOf is that in TypeScript, there isn't support for an XOR type operator, meaning there is no direct correspondence between the semantics of OneOf and what can be adequately expressed in the TypeScript language at a type level.
The closest approximation of OneOf would simply be Union (as per reference prototype implementation), however a more correct implementation would be to generate a Never
schematic if the constituents of the OneOf had potential for more than one match. Consider.
type T = string ^ string // T is never (imaginary ^ as xor operator)
const T = Type.UnionOneOf([ // this schema is illogical as passing a string
Type.String(), // would fail due to multiple matches, and not
Type.String() // passing a string would fail also, making the
]) // only reasonable type TNever
If TypeBox were to implement OneOf, it would need to have the characteristics above (i.e. detect structural overlap and yield never for illogical cases), however the complexity and compute cost deriving this for more complex types is currently unknown and presumed prohibitively expensive for more complex data structures with constraints (but the project welcomes community experimentation trying to compute the type)
Custom OneOf
were there any work arounds that people have been using?
Yes, as mentioned, if you need OneOf, you can use the UnionOneOf implementation (but mindful not to construct an illogical schema). However you will need to include this as a module somewhat in your project.
import { TypeRegistry, Kind, Static, TSchema, SchemaOptions } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
// -------------------------------------------------------------------------------------
// TUnionOneOf
// -------------------------------------------------------------------------------------
export interface TUnionOneOf<T extends TSchema[]> extends TSchema {
[Kind]: 'UnionOneOf'
static: { [K in keyof T]: Static<T[K]> }[number]
oneOf: T
}
// -------------------------------------------------------------------------------------
// UnionOneOf
// -------------------------------------------------------------------------------------
/** `[Experimental]` Creates a Union type with a `oneOf` schema representation */
export function UnionOneOf<T extends TSchema[]>(oneOf: [...T], options: SchemaOptions = {}) {
function UnionOneOfCheck(schema: TUnionOneOf<TSchema[]>, value: unknown) {
return 1 === schema.oneOf.reduce((acc: number, schema: any) => (Value.Check(schema, value) ? acc + 1 : acc), 0)
}
if (!TypeRegistry.Has('UnionOneOf')) TypeRegistry.Set('UnionOneOf', UnionOneOfCheck)
return { ...options, [Kind]: 'UnionOneOf', oneOf } as TUnionOneOf<T>
}
const T = UnionOneOf([
Type.String(),
Type.Number(),
Type.Boolean(),
])
However, in most cases, it's generally just easier to use Union (anyOf)
Dependencies, Conditional Sub Schemas
TypeBox may implement these (starting with If/Else/Then schematics) when it makes a shift to the draft 2012-12 specification. You can use conditional / dependent schemas today (using similar techniques above, or just using Unsafe), however you will need to use Ajv to validate (as the TypeBox compiler will only check for constructs it can represent).
Hope this helps!
S
from typebox.
@nicholasfinos Hiya,
Might close off this issue as UnionOneOf isn't planned as a standard type for the reasons noted above (at least in the near term). But if you need some help expressing this type (or the dependencies keyword) using TypeBox, feel free to ping me on this thread. Can provide additional assistance if you need.
All the best!
S
from typebox.
@nicholasfinos
I believe for your use case, something like this might work
const formValues = Type.Union([
Type.Object({
state: Type.Literal('State 1'),
region: Type.Union([Type.Literal('Region 1'), Type.Literal('Region 2')])
}),
Type.Object({
state: Type.Literal('State 2'),
region: Type.Union([Type.Literal('Region 3'), Type.Literal('Region 4')])
})
])
Not sure if this is related, but after looking around and not finding much for what I'm attempting to do, I stumbled on a solution that solves my particular case of allowing one key or another key only pretty well. The particular case is in allowing one key or another and never both keys (XOR) and it can be achieved by doing this:
const extraCond = Type.Union([
Type.Object({ param1: Type.Any(), param2: Type.Optional(Type.Never()) }),
Type.Object({ param1: Type.Optional(Type.Never()), param2: Type.Any() }),
])
For my use case on creating recurrence objects for scheduling events, I do something like the following which seems to work well, Intellisense and fastify schema validation seems to work as expected
const dailyCondition = Type.Object({
interval: Type.Number(),
freq: Type.Literal('daily'),
})
const weeklyCondition = Type.Object({
interval: Type.Number(),
freq: Type.Literal('weekly'),
byDay: Type.Array(Type.String()),
})
const monthlyCondition = Type.Union([
Type.Object({
interval: Type.Number(),
freq: Type.Literal('monthly'),
byMonthDay: Type.Number(),
}),
Type.Object({
interval: Type.Number(),
freq: Type.Literal('monthly'),
byDay: Type.Array(Type.String(), { minItems: 1, maxItems: 1 }),
bySetPos: Type.Number(),
}),
])
const endOnCondition = Type.Union([
Type.Object({
count: Type.Optional(Type.Never()),
until: Type.Optional(Type.Never()),
}),
Type.Object({
count: Type.Number(),
until: Type.Optional(Type.Never()),
}),
Type.Object({
count: Type.Optional(Type.Never()),
until: Type.String({ format: 'date-time' }),
}),
])
const recurrence = Type.Intersect([
Type.Union([dailyCondition, weeklyCondition, monthlyCondition]),
endOnCondition,
])
from typebox.
Related Issues (20)
- Regression bug 0.32.19 HOT 2
- TypeGuard.IsEnum HOT 2
- Better pre-validation errors for invalid TypeBox schema HOT 2
- Static resolution is not working as expected when using a generic function to generate a schema HOT 3
- tsconfig "strict": true - ignore modifiers HOT 2
- `Static` types cannot be used to enforce types from a generic parameter HOT 3
- Applying refinements + general advice on my implementation HOT 11
- Unable to create value of custom class type HOT 2
- Migrate to regexp vs string type in pattern HOT 4
- feedback on Refinement API HOT 2
- Type.Date() cause errors with fastify and querybuilder returning type Date HOT 2
- Should coerce type to array HOT 2
- Interconnected interfaces, missing type annotations and maximum length exceeded. HOT 2
- Optimisations to inference in 0.32 seems to cause `ts(2589)` with composite types. HOT 16
- Add trim option to Type.String() HOT 1
- Should add option to remove redundant prop in Convert() HOT 1
- Metadata missing in custom types HOT 2
- Schema of JSON Schema HOT 3
- Utility type inference appears missing or broken HOT 2
- How to express function optional parameter? HOT 5
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.