question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[docs] relation between JTDSchemaType and JTDDataType

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:26 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
erikbrinkmancommented, May 21, 2021

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-L41

Finally, and unfortunately, there can’t be a nice correspondence between JTDSchemaType and JTDDataType 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 outset JTDDataType was not designed to handle unions, because it’s not clear how they should be handled. For the same reason that JTDDataType<{ type: "timestamp" }> isn’t Date but Date | string. These ambiguities prevent a bijection between JTD schema- and data-space, and therefore prevent the kind of checking you desire.

1reaction
erikbrinkmancommented, Jul 18, 2021

closing due to inactivity, but feel free to reopen if this isn’t resolved @josh-hemphill

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found