Comments (6)
+1 would love this feature as well 🙂
from typebox.
Hi everyone. Will be closing off this issue shortly as I've just published a new version of TypeBox that deprecates Type.Dict(...)
in favor of a new Type.Record(K, V)
type which is analogous to TypeScript's utility type Record<Keys, Value>
which you can find information on here.
The Type.Record(...)
type works the same as the Type.Dict(...)
except you can now pass a type for the Key
. Valid key types are Type.String()
, Type.Number()
and Type.Union([...Type.Literal(...)])
String Keys
type T = Record<string, number> // typescript
const T = Type.Record(Type.String(), Type.Number()) // typebox - replaces Type.Dict()
type T = { [key: string]: number } // static
Number Keys
type T = Record<number, number> // typescript
const T = Type.Record(Type.Number(), Type.Number()) // typebox
type T = { [key: number]: number } // static
Literal Keys
type T = Record<'a' | 'b' | 'c', number> // typescript
const K = Type.Union([
Type.Literal('a'),
Type.Literal('b'),
Type.Literal('c'),
])
const T = Type.Record(K, Type.Number()) // typebox
type T = {
a: number,
b: number,
c: number
} // static
Unfortunately, I have not found a solution to allow Type.Object(...)
to intersect how I would prefer for Type.Dict(...)
or Type.Record(...)
types as JSON schema can't adequately validate in the same way as TypeScript can intersect. However, I've removed the TS constraint that prevent Type.Record()
from being intersected, and there are some compositions that work well so long as you keep unevaluatedProperties
and additionalProperties
as undefined
. Mileage may vary, but you're free to try :)
Thanks for raising this issue. It's been quite the journey figuring out how to provide better support for this, so I hope the new Type.Record(...)
serves as a reasonable compromise given the constraints.
Many Thanks
S
from typebox.
@lazharichir Have taken another look at this tonight and I don't think this is a trivially resolvable issue. I have explored a couple of approaches, one constraining to TObject<TProperties> | TDict<...>
(which lead to a considerable amount of verbosity trying to infer from the Dict
case) and another (currently sitting inside the unevaluatedProperties
branch) which explores a 2019
draft proposal that would allow for a more tenable schema representation for Intersect
. However it doesn't solve the Dict
case where the expectation is you have some
defined properties, with the additionalProperties
conforming to the Dict
.
Am going to move this issue from bug
to enhancement
and explore some other options at some point down the road.
from typebox.
I'll check the new types out, thanks for trying your best.
Stay well,
L
from typebox.
@lazharichir Hi, good spotting. There has recently been some updates to Intersect
handling in TypeBox where the Type.Intersect(...)
function now constrains to TObject<TProperties>
types only. This was due to a recent schema update where additionalProperties: false
is set for all objects
(which implicated the previous intersect handling).
Note a Type.Dict(...)
when intersected with a Type.Object(...)
implies additionalProperties: true
which is where this issue originates. However, your implementation looks good to me, so I'm going to flag this as a bug. I may not be able to look at this for a week or so. As a work around, could something like the following work for you?
const Metadata = Type.Object({
provider: Type.Union([Type.Literal(`gcs`), Type.Literal(`s3`)]),
bucket: Type.String({ minLength: 1 }),
name: Type.String({ minLength: 1 }),
extra: Type.Dict(Type.Any()) // should work ok
})
Thanks for letting me know!
S
from typebox.
No worries! In the meantime I will use an untyped Dict and will type it later once this is fixed.
Thanks for the quick reply @sinclairzx81 !
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
- Composite working uncorrect 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.