JTDSchemaType has Typescript error with valid JTD
See original GitHub issueWhat version of Ajv are you using? Does the issue happen if you use the latest version?
"ajv": "^8.8.2",
** Code and error **
import Ajv, { JTDSchemaType } from 'ajv/dist/jtd'
const ajv = new Ajv()
// then
export interface JumpSpec<P = unknown, T = unknown> {
type: string
uri: string
param?: P
mountAt?: string
defaults?: T[] // this array will caus the problem
}
const spec: JTDSchemaType<JumpSpec> = {
properties: {
type: { type: 'string' },
uri: { type: 'string' },
},
optionalProperties: {
param: {},
mountAt: { type: 'string' },
defaults: { elements: {} }, // <-- error
},
}
Typescript objects to ‘elements’ with : (property) elements: {} Type '{}' is not assignable to type 'never'
Looking at the spec: https://datatracker.ietf.org/doc/html/rfc8927#section-2
I think elements can take the empty form from the CDDL:
; All JTD schemas are JSON objects taking on one of eight forms
; listed here.
schema = (
ref //
type //
enum //
elements //
properties //
values //
discriminator //
empty //
)
...
; elements describes the "elements" schema form.
elements = ( elements: { schema }, shared )
Which makes me think it’s a bug 😃
Thanks for your awesome validation library!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Typescript JTD examples fail with "is not assignable to type ...
This is the complete code I am trying to compile. ... No JSON formatted schema is used. The schema is a javascript object...
Read more >Using with TypeScript - Ajv JSON schema validator
when utility type is used, compiled JTD serializers only accept data of correct type (as they do not validate that the data 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 >Learn JSON Typedef in 5 Minutes
JSON Type Definition (aka “JSON Typedef”, or just “JTD”) schemas are just JSON documents ... Technically, only the JSON representation of a schema...
Read more >TypeScript Type Safety with AJV Standalone
TypeScript (TS) is a statically-typed safe language that outputs ... valid = ajv.validate(schema, data) if (!valid) console.log(ajv.errors).
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
What you’re writing makes sense, but is actually nonsense 😅 . Typescript doesn’t allow parametrized types to actually be used, you need to make them “concrete”. In this example, both
P
andT
have default definitions, so you can think of it like:in JTD, the correct schema for
Record<string, unknown>
is{ values: {} }
, thus if you fix your schema to be:it then compiles fine.
Note: this was called out in the error which is saying that
{}
is missing the keyvalues
to be compatible withJTDSchemaType<Record<string, unknown>>
.By not using
JTDSchemaType
you’re implicitly usingJTDDataType
under the hood, which should be parsingparam
anddefaults
as unknowns instead of as records. That’s fine, but different from the interface you specified.It’s probably also worth noting that JTD doesn’t have a way to specify
Record<string, never>
which seems like it was maybe what you intended. For that you’ll need a more powerful verifier (like JSON Schema) to guarantee that there are no keys.@epoberezkin @erikbrinkman Thank you, this is super helpful!