Skip to content

Commit 92e1ee2

Browse files
authored
Merge pull request #56 from ferflores507/dev
Dev
2 parents a9ae7ef + fa77823 commit 92e1ee2

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ferflores507/object-builder",
3-
"version": "9.6.0",
3+
"version": "9.7.0",
44
"main": "index.js",
55
"scripts": {
66
"test": "vitest"

src/builders/Operators.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ export class Operators implements WithTaskOptions<Operators> {
3131
}
3232
assign: OperatorTask = (current, previous) => Object.assign(current, previous)
3333
boolean = (value: any) => !!value
34+
childrenSchema = (childrenSchema: Record<string, any>, path: Path) => {
35+
const schemas = []
36+
let schema = null
37+
38+
for(const name of path) {
39+
const { setup, schema: currentSchema, children } = childrenSchema[name]
40+
41+
if(setup) {
42+
schemas.push(setup)
43+
}
44+
45+
schema = currentSchema
46+
childrenSchema = children
47+
}
48+
49+
schemas.push(schema)
50+
51+
return schemas
52+
}
3453
date = (value: any, options: Intl.DateTimeFormatOptions & { locale: string }) => {
3554
return new Date(value).toLocaleString(options.locale, options)
3655
}

src/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export type Schema = Partial<{
9494
boolean: true
9595
consulta: Consulta
9696
call: string | Propiedades | string[]
97+
childrenSchema: SchemaDefinition
9798
const: any
9899
date: Schema | SchemaPrimitive
99100
debounce: Schema | SchemaPrimitive

tests/schema/mixed.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,87 @@ import { Queue } from '../../src/helpers/Queue'
88
import { TaskBuilder } from '../../src/builders/TaskBuilder'
99
import { Propiedades } from '../../src/models'
1010

11+
describe("children schema", () => {
12+
const childrenSchema = {
13+
a: {
14+
setup: {
15+
init: {
16+
details: {
17+
const: {
18+
id: 1,
19+
name: " one "
20+
}
21+
}
22+
}
23+
},
24+
schema: {
25+
const: "path a"
26+
},
27+
children: {
28+
a1: {
29+
schema: {
30+
path: "$details.name",
31+
trim: true
32+
}
33+
},
34+
a2: {
35+
schema: {
36+
path: "$details.name"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
const a = childrenSchema.a
44+
45+
const cases = [
46+
{
47+
path: ["a"],
48+
expected: [
49+
a.setup,
50+
a.schema
51+
],
52+
expectedResult: "path a"
53+
},
54+
{
55+
path: ["a", "a1"],
56+
expected: [
57+
a.setup,
58+
a.children.a1.schema
59+
],
60+
expectedResult: "one"
61+
},
62+
{
63+
path: ["a", "a2"],
64+
expected: [
65+
a.setup,
66+
a.children.a2.schema
67+
],
68+
expectedResult: a.setup.init.details.const.name
69+
}
70+
]
71+
72+
test.each(cases)("children schema path: $path", async ({ path, expected, expectedResult }) => {
73+
const schema = {
74+
const: childrenSchema,
75+
childrenSchema: {
76+
const: path
77+
}
78+
}
79+
80+
await expectToEqualAsync({ schema, expected })
81+
82+
const builder = new ObjectBuilder().withSchema(schema)
83+
84+
const result = builder
85+
.withSchema({ reduce: builder.build() })
86+
.build()
87+
88+
expect(result).toEqual(expectedResult)
89+
})
90+
})
91+
1192
test("propiedades $bind", async () => {
1293
await expectToEqualAsync({
1394
store: {

0 commit comments

Comments
 (0)