You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently experimented with RJSF, a cool library that generates forms out of JSON Schemas. I needed to validate some objects conditionally, such as in this example.
So I used the dependencies keyword, as advised.
Issue
However, unfortunately, as far as I understand, this keyword is not supported in json-schema-to-ts, which means I have to redefine my types manually...
For example, taking the example from RJSF docs:
importtype{RJSFSchema}from"@rjsf/utils";importtype{FromSchema}from"json-schema-to-ts";constschema={title: "Person",type: "object",properties: {"Do you have any pets?": {type: "string",enum: ["No","Yes: One","Yes: More than one"],default: "No",},},required: ["Do you have any pets?"],dependencies: {"Do you have any pets?": {oneOf: [{properties: {"Do you have any pets?": {enum: ["No"],},},},{properties: {"Do you have any pets?": {enum: ["Yes: One"],},"How old is your pet?": {type: "number",},},required: ["How old is your pet?"],},{properties: {"Do you have any pets?": {enum: ["Yes: More than one"],},"Do you want to get rid of any?": {type: "boolean",},},required: ["Do you want to get rid of any?"],},],},},}asconstsatisfiesRJSFSchema;typeMyType=FromSchema<typeofschema>;
This type inferred by json-schema-to-ts is currently:
typeMyType={[x: string]: unknown;"Do you have any pets?": "No"|"Yes: One"|"Yes: More than one";}
(everything in dependencies is ignored).
Basic solution
As a basic solution I would expect the resulting type to be:
typeMyType={[x: string]: unknown;"Do you have any pets?": "No"|"Yes: One"|"Yes: More than one";"How old is your pet?"?: number;// can be undefined"Do you want to get rid of any?"?: boolean;// can be undefined}
More advanced solution
In an ideal world, we could have a type closely matching the validation logic, like:
typeMyType=|{[x: string]: unknown;"Do you have any pets?": "No";}|{[x: string]: unknown;"Do you have any pets?": "Yes: One";"How old is your pet?": number;}|{[x: string]: unknown;"Do you have any pets?": "Yes: More than one";"Do you want to get rid of any?": boolean;};
However, I guess the implementation for this advanced solution would be pretty far from trivial.
Question
I was wondering:
if anyone had encountered the same issue (it's kind of niche I guess)
if anyone was be interested to help me implement this this feature, starting with the basic solution
Apparently this keyword will be deprecated in the next draft, but the behaviour that I'm describing will be transferred to the dependentSchemas keyword, so the migration would not be too complicated.
The text was updated successfully, but these errors were encountered:
Hello!
I recently experimented with RJSF, a cool library that generates forms out of JSON Schemas. I needed to validate some objects conditionally, such as in this example.
So I used the
dependencies
keyword, as advised.Issue
However, unfortunately, as far as I understand, this keyword is not supported in
json-schema-to-ts
, which means I have to redefine my types manually...For example, taking the example from RJSF docs:
This type inferred by
json-schema-to-ts
is currently:(everything in
dependencies
is ignored).Basic solution
As a basic solution I would expect the resulting type to be:
More advanced solution
In an ideal world, we could have a type closely matching the validation logic, like:
However, I guess the implementation for this advanced solution would be pretty far from trivial.
Question
I was wondering:
WDYT?
Notes
dependencies
Apparently this keyword will be deprecated in the next draft, but the behaviour that I'm describing will be transferred to the
dependentSchemas
keyword, so the migration would not be too complicated.The text was updated successfully, but these errors were encountered: