[docs] relation between JTDSchemaType and JTDDataType
See original GitHub issueWhat version of Ajv are you using? 8.4.0
Does the issue happen if you use the latest version? Yes
JTD Schema
export interface REQ {
data: {
text: string,
}
};
export const Req: JTDSchemaType<REQ> = {
properties: {
data: {
properties: {
text: { type: 'string' },
},
},
},
};
Your code
type ReqD = JTDDataType<typeof Req>
/* {
data: {
text: unknown;
} & {};
} & {} */
type ReqD2 = JTDDataType<typeof Req.properties.data>
/* {
text: unknown;
} & {}; */
What results did you expect?
JTDDataType<typeof Req.properties.data>
// { text: string }
Are you going to resolve the issue?
If this is a bug, and I know where to start, sure.
Issue Analytics
- State:
- Created 2 years ago
- Comments:26 (10 by maintainers)
Top Results From Across the Web
JSON Type Definition | Ajv JSON schema validator
The type JTDSchemaType can be used to validate that the written schema matches the type you expect to validate. This type is strict...
Read more >What needs to happen before we can get simultaneous TS ...
8 The last I've seen of work on the JTDSchemaType and JTDDataType is in #1489 But the docs still say type inference is...
Read more >Can i just paste jtd as typescript type syntax? - Stack Overflow
JTDDataType is a utility type from ajv, which converts a jtd schema type to the related valid types in typescript. Security notes:.
Read more >JSON Type Definition
JSON Type Definition, aka RFC 8927, is an easy-to-learn, portable, and standardized way to describe the shape of your JSON data. user.jtd.json.
Read more >Ajv JSON schema validator - npm
More than 100 people contributed to Ajv, and we would love to have ... that will benefit many users and ideas to improve...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for working through this mostly without bugging me, but feel free to tag me earlier @epoberezkin. There’s a few things to address.
First, for discriminated unions, JTD spec only allows strings. If you change your “ok” numbers to strings, the schema checks out. This isn’t normal javascript nonsense where types get coerced all the time.
Second, there is a
SomeJTDSchemaType
to check the general schema objects: https://github.com/ajv-validator/ajv/blob/be07d3d8c0aac8fffe68378d25bdd892c61ba2e5/lib/types/jtd-schema.ts#L8-L41Finally, and unfortunately, there can’t be a nice correspondence between
JTDSchemaType
andJTDDataType
and the answer has to do with unions.JTDSchemaType<string>
isn’t{ type: "string" }
(even ignoring metadata), it’s{ type: "string" | "timestamp" }
due to the way that schemas are handled. From the outsetJTDDataType
was not designed to handle unions, because it’s not clear how they should be handled. For the same reason thatJTDDataType<{ type: "timestamp" }>
isn’tDate
butDate | string
. These ambiguities prevent a bijection between JTD schema- and data-space, and therefore prevent the kind of checking you desire.closing due to inactivity, but feel free to reopen if this isn’t resolved @josh-hemphill