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

[JSONSchemaBridge] Export resolveIfNeeded function #1307

Closed
lordrip opened this issue Feb 19, 2024 · 3 comments
Closed

[JSONSchemaBridge] Export resolveIfNeeded function #1307

lordrip opened this issue Feb 19, 2024 · 3 comments
Assignees
Labels
Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package Type: Feature New features and feature requests

Comments

@lordrip
Copy link

lordrip commented Feb 19, 2024

Currently, we have partial support for oneOf keywords in uniforms, and as discussed in #863, one of the proposed workarounds is to process the schema before passing it to the form.

This can easily done for schemas that don't use $ref, in case a reference schema is used, it would be beneficial to have available the resolveRefIfNeeded function

The goal for this issue is to export the aforementioned function, either through a standalone export or as a protected method of the JSONSchemaBridge class

@lordrip
Copy link
Author

lordrip commented Feb 19, 2024

If this is approved, I can provide a PR exporting this function 😃

@kestarumper kestarumper self-assigned this Feb 26, 2024
@github-project-automation github-project-automation bot moved this to Needs triage in Open Source Feb 26, 2024
@kestarumper kestarumper added Type: Feature New features and feature requests Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package labels Feb 26, 2024
@kestarumper
Copy link
Member

kestarumper commented Feb 26, 2024

Hi @lordrip,
Currently, this function is internal, and we can change it however we want without considering backward compatibility. If we export it, we will have to commit to its signature and support it.

There is also #1278 on the plate, and resolveRefIfNeeded might play a role here.

Another workaround, for now, would be to just copy the function into your codebase because it's not that big:

function resolveRef(reference: string, schema: UnknownObject) {
invariant(
reference.startsWith('#'),
'Reference is not an internal reference, and only such are allowed: "%s"',
reference,
);
const resolvedReference = reference
.split('/')
.filter(part => part && part !== '#')
.reduce((definition, next) => definition[next] as UnknownObject, schema);
invariant(
resolvedReference,
'Reference not found in schema: "%s"',
reference,
);
return resolvedReference;
}
function resolveRefIfNeeded(
partial: UnknownObject,
schema: UnknownObject,
): UnknownObject {
if (!('$ref' in partial)) {
return partial;
}
const { $ref, ...partialWithoutRef } = partial;
return resolveRefIfNeeded(
// @ts-expect-error The `partial` and `schema` should be typed more precisely.
Object.assign({}, partialWithoutRef, resolveRef($ref, schema)),
schema,
);
}

I'm also considering going with export function experimental_resolveRefIfNeeded to mark that its API might break. That would be the middle ground between your needs and our commitment.

Let me know what you think.

@lordrip
Copy link
Author

lordrip commented Feb 26, 2024

Thanks for the answer @kestarumper.

I agree that exporting the function will increase the API surface area, I'll copy the function in the meantime, to move forward. I'll close the issue. Thanks once again for taking the time.

@lordrip lordrip closed this as completed Feb 26, 2024
@github-project-automation github-project-automation bot moved this from Needs triage to Closed in Open Source Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package Type: Feature New features and feature requests
Projects
Archived in project
Development

No branches or pull requests

2 participants