JSONSchemaType support for Record
See original GitHub issueVersion 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:
- Created 3 years ago
- Reactions:1
- Comments:11 (5 by maintainers)
Top 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 >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
A few points:
never
style type with custom errors so you can better indicate what the problem is.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.propertyNames?: JSONSchemaType<string>
so you could instead make itOmit< 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.required
should be optional whenRequiredMembers
is emptyhttps://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.
@epoberezkin ping to get your feedback on what you had in mind