Skip to content

Commit 55e3475

Browse files
chore: cleanup
1 parent d6afa5b commit 55e3475

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

next/src/utils.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,43 @@ function isObject(value: any): boolean {
6565
* @param schema2 - The second schema to merge
6666
*/
6767
export function deepMergeSchemas<T extends Record<string, any>>(schema1: T, schema2: T): void {
68-
// Handle null/undefined
68+
// Handle null/undefined values
6969
if (!schema1 || !schema2) {
7070
return
7171
}
7272

7373
// Handle non-objects
74-
if (typeof schema1 !== 'object' || typeof schema2 !== 'object')
74+
if (typeof schema1 !== 'object' || typeof schema2 !== 'object') {
7575
return
76+
}
7677

77-
// Merge all properties from obj2 into obj1
78-
for (const [key, value] of Object.entries(schema2)) {
79-
// let's skip merging conditionals such as if, oneOf, then ,else, allOf, anyOf, etc.
78+
// Merge all properties from schema2 into schema1
79+
for (const [key, schema2Value] of Object.entries(schema2)) {
80+
// let's skip merging some properties
8081
if (KEYS_TO_SKIP.includes(key)) {
8182
continue
8283
}
8384

84-
// If the value is an object, merge recursively
85-
if (isObject(value)) {
86-
// If both objects have this key and it's an object, merge recursively
87-
if (isObject(schema1[key])) {
88-
deepMergeSchemas(schema1[key], value)
85+
const schema1Value = schema1[key]
86+
87+
// If the value is an object:
88+
if (isObject(schema2Value)) {
89+
// If both schemas have this key and it's an object, merge recursively
90+
if (isObject(schema1Value)) {
91+
deepMergeSchemas(schema1Value, schema2Value)
8992
}
90-
// Otherwise, if the value is different, assign it
91-
else if (schema1[key] !== value) {
92-
schema1[key as keyof T] = value
93+
// Otherwise, if the value is different, just assign it
94+
else if (schema1Value !== schema2Value) {
95+
schema1[key as keyof T] = schema2Value
9396
}
9497
}
9598
// If the value is an array, cycle through it and merge values if they're different (take objects into account)
96-
else if (schema1[key] && Array.isArray(value)) {
97-
const originalArray = schema1[key]
99+
else if (schema1Value && Array.isArray(schema2Value)) {
100+
const originalArray = schema1Value
98101
// If the destiny value exists and it's an array, cycle through the incoming values and merge if they're different (take objects into account)
99-
for (const item of value) {
102+
for (const item of schema2Value) {
100103
if (item && typeof item === 'object') {
101-
deepMergeSchemas(originalArray, value)
104+
deepMergeSchemas(originalArray, schema2Value)
102105
}
103106
// "required" is a special case, it only allows for new elements to be added to the array
104107
else if (key === 'required') {
@@ -109,12 +112,13 @@ export function deepMergeSchemas<T extends Record<string, any>>(schema1: T, sche
109112
}
110113
// Otherwise, just assign it
111114
else {
112-
schema1[key as keyof T] = value as T[keyof T]
115+
schema1[key as keyof T] = schema2Value as T[keyof T]
113116
}
114117
}
115118
}
116-
else if (schema1[key] !== value) {
117-
schema1[key as keyof T] = value
119+
// Finally, if the value is different, just assign it
120+
else if (schema1[key] !== schema2Value) {
121+
schema1[key as keyof T] = schema2Value
118122
}
119123
}
120124
}

next/test/fields.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { TypeName } from 'json-schema-typed'
44
import { buildFieldSchema as buildField } from '../src/field/schema'
55

66
describe('fields', () => {
7+
/**
8+
* Auxiliary test function to build a field from a schema (consider the schema and original schema as the same)
9+
*/
710
function buildFieldSchema(schema: JsfSchema, name: string, required: boolean = false, strictInputType?: boolean, type?: JsfSchemaType) {
811
return buildField({
912
schema,

0 commit comments

Comments
 (0)