Skip to content

Commit 46a6afc

Browse files
authored
Merge pull request #58 from ferflores507/dev
Dev
2 parents 5e3620e + 3e465db commit 46a6afc

File tree

6 files changed

+84
-4
lines changed

6 files changed

+84
-4
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.7.1",
3+
"version": "9.8.0",
44
"main": "index.js",
55
"scripts": {
66
"test": "vitest"

src/builders/ObjectBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export class ObjectBuilder implements Builder {
184184
? { definition, task: options }
185185
: {
186186
...options,
187-
definition: options.transform(definition),
187+
definition: options.transform(definition, this),
188188
})
189189
}
190190

src/builders/Operators.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ export class Operators implements WithTaskOptions<Operators> {
170170
return Object.fromEntries(filteredEntries)
171171
}
172172
}
173+
validate = {
174+
transform: (schema: SchemaDefinition, builder: Builder) : Schema => {
175+
const newSchema = Array.isArray(schema)
176+
? schema
177+
: builder.with({ schema }).build()
178+
179+
return {
180+
const: {
181+
result: builder.with({ schema: newSchema }).build(),
182+
schema: newSchema
183+
}
184+
}
185+
},
186+
task: (initial: any, { result, schema }: { result: any, schema: SchemaDefinition }) => {
187+
const [results, schemas] = [result, schema].map(toArray)
188+
189+
return results.every(Boolean) || schemas.filter((el, i) => !results[i])
190+
}
191+
}
173192
join = {
174193
task: (source: [], separator: any) => source.join(separator),
175194
transform: (schema: any) => schema === true ? "" : schema

src/helpers/varios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export const tryCopy = (obj: {}) => {
363363
return obj
364364
}
365365

366-
export const toArray = <T>(value: T) => Array.isArray(value) ? value : [value]
366+
export const toArray = <T>(value: T | T[]) => Array.isArray(value) ? value : [value]
367367

368368
export const toArrayOrNull = (value: any) => value != null ? toArray(value) : null
369369

src/models.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type OperatorTask = (current: any, previous: any, builder: Builder) => an
3737

3838
export type TaskOptions = OperatorTask | {
3939
task: OperatorTask,
40-
transform: (schema: Schema) => any
40+
transform: (schema: Schema, builder: Builder) => any
4141
}
4242

4343
export type WithTaskOptions<T> = { [key in keyof T]: TaskOptions }
@@ -174,6 +174,7 @@ export type Schema = Partial<{
174174
unpackAsGetters: SchemaDefinition
175175
use: string
176176
UUID: true
177+
validate: SchemaDefinition
177178
values: true
178179
with: Schema
179180
}> & ArraySchema

tests/schema/mixed.test.ts

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

11+
describe("validate", () => {
12+
const validate = [
13+
{
14+
path: "id",
15+
greaterThan: 6,
16+
lessThan: 8
17+
},
18+
{
19+
path: "name.length",
20+
greaterThan: 4,
21+
lessThan: 10
22+
}
23+
]
24+
25+
test("validate with explicit schema", async () => {
26+
await expectToEqualAsync({
27+
store: {
28+
id: 6,
29+
name: "Mel",
30+
age: 5
31+
},
32+
schema: {
33+
validate: {
34+
const: validate
35+
}
36+
},
37+
expected: validate
38+
})
39+
})
40+
41+
test("validate returns array of failed schemas", async () => {
42+
await expectToEqualAsync({
43+
store: {
44+
id: 6,
45+
name: "Mel",
46+
age: 5
47+
},
48+
schema: {
49+
validate
50+
},
51+
expected: validate
52+
})
53+
})
54+
55+
test("validate returns true", async () => {
56+
await expectToEqualAsync({
57+
store: {
58+
id: 7,
59+
name: "Melany",
60+
age: 5
61+
},
62+
schema: {
63+
validate
64+
},
65+
expected: true
66+
})
67+
})
68+
69+
})
70+
1171
describe("children schema", () => {
1272
const childrenSchema = {
1373
a: {

0 commit comments

Comments
 (0)