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.

JSONSchemaType support for Record

See original GitHub issue

Version v8

Problem Currently JSONSchemaType does not support types like Record<string, T | undefined> (or {[K in string]?: T}).

Solution

For the type for example:

type MyRecord = Record<string, number | undefined>

JSONSchema should be:

const myRecordSchema: JSONSchemaType<MyRecord> = {
  type: "object"
  additionalProperty: {type: "number"}
}

It should also allow propertyName keyword.

Possibly, below should also type check (although it can be expressed with properties but it would be more verbose:

type MyEnumRecord = Record<"a" | "b" | "c" | "d", number | undefined>

const myEnumRecordSchema: JSONSchemaType<MyEnumRecord> = {
  type: "object"
  propertyNames: {enum: ["a", "b", "c", "d"]}, // it won't check for completeness
  additionalProperty: {type: "number"}
}

(Although it would require properly supporting enum, which currently is not type checked at all)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
erikbrinkmancommented, Apr 15, 2021

A few points:

  1. Typescript errors aren’t great. I agree. Unfortunately there’s not a great way to customize them. There are some proposals of making a never style type with custom errors so you can better indicate what the problem is.
  2. The way JSONSchemaType is implemented, all of these extra parameters are partial, so they can exist or not, but there’s no guarantee that you fully specify the type, which is probably fine.
  3. For your (2), it’s currently propertyNames?: JSONSchemaType<string> so you could instead make it Omit< JSONSchemaType<string>, "type"> & { type?: "string" }. Which would just make type optional. Note that current property names does no validation, so you can specify an enum, but you can also put whatever you want in the enum.
  4. For your (1), are you just thinking that required should be optional when RequiredMembers is empty

https://github.com/ajv-validator/ajv/blob/8762e6f8b17c4e61f3d47db502b19e99f4582724/lib/types/json-schema.ts#L138-L140

I agree that you could open new issues, but I think both of these are potentially pretty simple.

1reaction
erikbrinkmancommented, Apr 11, 2021

@epoberezkin ping to get your feedback on what you had in mind

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Type Definition | Ajv JSON schema validator
Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type ... This form defines record (JSON object) that has defined required and optional ...
Read more >
Data Types in Records | Airbyte Documentation
This is supported with JSON schema's oneOf and anyOf types. Note that JsonSchema's allOf combining structure is not accepted within the protocol, because...
Read more >
JSONSchema to TypeScript - Defining a record of strings to ...
I'm using json-schema-to-typescript and would like to specify an array of strings. Currently, I have the following: "type": "object ...
Read more >
Structuring a complex schema — Understanding JSON ...
For this example, let's say we want to define a customer record, where each customer may have both a shipping and a billing...
Read more >
JSON Schema Serializer and Deserializer
JSON Schema Deserializer¶. Plug KafkaJsonSchemaDeserializer into KafkaConsumer to receive messages of any JSON Schema type from Kafka.
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