Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"allOf" Support for the custom JSON schema keywords merging #3258

Open
1 task done
Dzuming opened this issue Nov 22, 2022 · 1 comment
Open
1 task done

"allOf" Support for the custom JSON schema keywords merging #3258

Dzuming opened this issue Nov 22, 2022 · 1 comment

Comments

@Dzuming
Copy link

Dzuming commented Nov 22, 2022

Prerequisites

What theme are you using?

core

Is your feature request related to a problem? Please describe.

In a JSON schema, there is a possibility to use if else then. I want to define a JSON schema with a custom keyword

...
        "properties": {
          "hasDescription": {
            "type": "boolean"

          },
          "description": {
            "type": "string",
            "customProperty": {
              "isImportant": false
            }
          },
        }
...

"allOf": [
          {
            "if": {
              "properties": {
                "hasDescription": {
                  "const": true
                }
              }
            },
            "then": {
              "properties": {
                "description": {
                  "customProperty": {
                    "isImportant": true
                  }
                }
              }
            },
            "else": {}
          }
        ]

If all of the conditions are met in the form, there is a warning in the console, and values are not merged:

could not merge subschemas in allOf:
Error: No resolver found for key customProperty. You can provide a resolver for this keyword in the options, or provide a default resolver

Describe the solution you'd like

RJSF library uses json schema merge all library under the hood to merge subschemas. There is an option to add custom resolvers via options property .

Now, packages/utils/src/schema/retrieveSchema.ts code where merging happens looks like

 if (ALL_OF_KEY in schema) {
    try {
      resolvedSchema = mergeAllOf({
        ...resolvedSchema,
        allOf: resolvedSchema.allOf,
      }) as S;
    } catch (e) {
      console.warn("could not merge subschemas in allOf:\n" + e);
      const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
      return resolvedSchemaWithoutAllOf as S;
    }
  }

The idea is to be able to provide custom mergeAllOf resolvers

resolvedSchema = mergeAllOf({
        ...resolvedSchema,
        allOf: resolvedSchema.allOf,
      }, 
      {
          resolvers: customResolvers
) as S;

So the end result will be like

resolvedSchema = mergeAllOf({
        ...resolvedSchema,
        allOf: resolvedSchema.allOf,
      }, 
      {
          resolvers: {
            customProperty: function (values, path, mergeSchemas, options) {
              return { ...values[0], ...values[1] };
            },
          },
        }
) as S;

Describe alternatives you've considered

No response

@Dzuming Dzuming added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Nov 22, 2022
@heath-freenome
Copy link
Member

@Dzuming are you willing to work with us to build this new feature? We are a small team with barely any time to fix bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants