Skip to content

Commit

Permalink
toIdSchema iterative
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Sep 3, 2024
1 parent a7b25e8 commit 8502b90
Showing 1 changed file with 127 additions and 40 deletions.
167 changes: 127 additions & 40 deletions packages/utils/src/schema/toIdSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,142 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
formData?: T,
_recurseList: S[] = []
): IdSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData);
const sameSchemaIndex = _recurseList.findIndex((item) => isEqual(item, _schema));
if (sameSchemaIndex === -1) {
return toIdSchemaInternal<T, S, F>(
const firstState = {
validator,
schema,
idPrefix,
idSeparator,
id,
rootSchema,
formData,
_recurseList,
idSchema: {} as IdSchema<T>,
};
const stack = [firstState];
while (stack.length > 0) {
const { validator, schema, rootSchema, _recurseList, formData, idPrefix, idSeparator, id, idSchema } = stack.pop()!;
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData);
const sameSchemaIndex = _recurseList.findIndex((item) => isEqual(item, _schema));
if (sameSchemaIndex === -1) {
stack.push({
validator,
schema: _schema,
idPrefix,
idSeparator,
id,
rootSchema,
formData,
_recurseList: _recurseList.concat(_schema),
idSchema,
});
continue;
}
}

if (ITEMS_KEY in schema && !get(schema, [ITEMS_KEY, REF_KEY])) {
stack.push({
validator,
_schema,
schema: get(schema, ITEMS_KEY) as S,
idPrefix,
idSeparator,
id,
rootSchema,
formData,
_recurseList.concat(_schema)
);
_recurseList,
idSchema,
});
continue;
}
}
if (ITEMS_KEY in schema && !get(schema, [ITEMS_KEY, REF_KEY])) {
return toIdSchemaInternal<T, S, F>(
validator,
get(schema, ITEMS_KEY) as S,
idPrefix,
idSeparator,
id,
rootSchema,
formData,
_recurseList
);
}
const $id = id || idPrefix;
const idSchema: IdSchema<T> = { $id } as IdSchema<T>;
if (getSchemaType<S>(schema) === 'object' && PROPERTIES_KEY in schema) {
for (const name in schema.properties) {
const field = get(schema, [PROPERTIES_KEY, name]);
const fieldId = idSchema[ID_KEY] + idSeparator + name;
(idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<T, S, F>(
validator,
isObject(field) ? field : {},
idPrefix,
idSeparator,
fieldId,
rootSchema,
// It's possible that formData is not an object -- this can happen if an
// array item has just been added, but not populated with data yet
get(formData, [name]),
_recurseList
);

idSchema[ID_KEY] = id || idPrefix;

if (getSchemaType<S>(schema) === 'object' && PROPERTIES_KEY in schema) {
for (const name in schema.properties) {
const field = get(schema, [PROPERTIES_KEY, name]);
const fieldId = idSchema[ID_KEY] + idSeparator + name;
const childIdSchema = {} as IdSchema<T>;
(idSchema as IdSchema<GenericObjectType>)[name] = childIdSchema;

stack.push({
validator,
schema: isObject(field) ? field : {},
idPrefix,
idSeparator,
id: fieldId,
rootSchema,
// It's possible that formData is not an object -- this can happen if an
// array item has just been added, but not populated with data yet
formData: get(formData, [name]),
_recurseList,
idSchema: childIdSchema,
});
}
}
}
return idSchema;

// if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
// const _schema = retrieveSchema<T, S, F>(
// validator,
// schema,
// rootSchema,
// formData,
// );
// const sameSchemaIndex = _recurseList.findIndex((item) =>
// isEqual(item, _schema)
// );
// if (sameSchemaIndex === -1) {
// return toIdSchemaInternal<T, S, F>(
// validator,
// _schema,
// idPrefix,
// idSeparator,
// id,
// rootSchema,
// formData,
// _recurseList.concat(_schema),
// );
// }
// }
//
// if (ITEMS_KEY in schema && !get(schema, [ITEMS_KEY, REF_KEY])) {
// return toIdSchemaInternal<T, S, F>(
// validator,
// get(schema, ITEMS_KEY) as S,
// idPrefix,
// idSeparator,
// id,
// rootSchema,
// formData,
// _recurseList,
// );
// }
//
// const $id = id || idPrefix;
// const idSchema: IdSchema<T> = { $id } as IdSchema<T>;
// if (getSchemaType<S>(schema) === "object" && PROPERTIES_KEY in schema) {
// for (const name in schema.properties) {
// const field = get(schema, [PROPERTIES_KEY, name]);
// const fieldId = idSchema[ID_KEY] + idSeparator + name;
// (idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<
// T,
// S,
// F
// >(
// validator,
// isObject(field) ? field : {},
// idPrefix,
// idSeparator,
// fieldId,
// rootSchema,
// // It's possible that formData is not an object -- this can happen if an
// // array item has just been added, but not populated with data yet
// get(formData, [name]),
// _recurseList,
// );
// }
// }
return firstState.idSchema;
}

/** Generates an `IdSchema` object for the `schema`, recursively
Expand Down

0 comments on commit 8502b90

Please sign in to comment.