vazco / uniforms

A React library for building forms from any schema.

Home Page:https://uniforms.tools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[JSONSchemaBridge] Export `resolveIfNeeded` function

lordrip opened this issue · comments

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

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

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.

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.