@cleverbrush/schema 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,8 +19,10 @@ const validateObject = async (obj, schema, validator) => {
19
19
  }
20
20
  if (typeof schema.preprocessors === 'object' && schema.preprocessors) {
21
21
  await Promise.all(Object.keys(schema.preprocessors).map(async (key) => {
22
+ if (key === '*')
23
+ return;
22
24
  if (typeof schema.preprocessors[key] === 'function') {
23
- obj[key] = await Promise.resolve(schema.preprocessors[key](key === '*' ? obj : obj[key]));
25
+ obj[key] = await Promise.resolve(schema.preprocessors[key](obj[key]));
24
26
  }
25
27
  else {
26
28
  const preprocessor = validator.preprocessors.get(schema.preprocessors[key]);
@@ -30,6 +32,19 @@ const validateObject = async (obj, schema, validator) => {
30
32
  obj[key] = await Promise.resolve(preprocessor(obj[key]));
31
33
  }
32
34
  }));
35
+ if ('*' in schema.preprocessors) {
36
+ const objectPreprocessor = schema.preprocessors['*'];
37
+ if (typeof objectPreprocessor === 'function') {
38
+ await Promise.resolve(objectPreprocessor(obj));
39
+ }
40
+ else {
41
+ const preprocessor = validator.preprocessors.get(objectPreprocessor);
42
+ if (typeof preprocessor !== 'function') {
43
+ throw new Error(`preprocessor '${objectPreprocessor}' is unknown`);
44
+ }
45
+ await Promise.resolve(preprocessor(obj));
46
+ }
47
+ }
33
48
  }
34
49
  if (typeof schema.properties === 'object' && schema.properties) {
35
50
  const errors = (await Promise.all(Object.entries(schema.properties).map(async ([name, schema]) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "keywords": [
5
5
  "object schema validator",
6
6
  "schema",
@@ -19,7 +19,7 @@
19
19
  "build": "tsc"
20
20
  },
21
21
  "dependencies": {
22
- "@cleverbrush/deep": "0.0.12"
22
+ "@cleverbrush/deep": "0.0.13"
23
23
  },
24
24
  "types": "./dist/index.d.ts"
25
25
  }
@@ -1377,6 +1377,7 @@ test('Preprocessors - 3', async () => {
1377
1377
  marriedAt: 'number'
1378
1378
  },
1379
1379
  preprocessors: {
1380
+ age: (value): number => 40,
1380
1381
  '*': (value: SomeType): void => {
1381
1382
  if (value.marriedAt > value.age) {
1382
1383
  value.marriedAt = value.age;
@@ -1390,7 +1391,7 @@ test('Preprocessors - 3', async () => {
1390
1391
  marriedAt: 80
1391
1392
  };
1392
1393
  await validator.validate(schema, obj);
1393
- expect(obj).toHaveProperty('marriedAt', 50);
1394
+ expect(obj).toHaveProperty('marriedAt', 40);
1394
1395
  });
1395
1396
 
1396
1397
  test('Preprocessors - 4', async () => {
@@ -31,10 +31,11 @@ export const validateObject = async (
31
31
  if (typeof schema.preprocessors === 'object' && schema.preprocessors) {
32
32
  await Promise.all(
33
33
  Object.keys(schema.preprocessors).map(async (key: string) => {
34
+ if (key === '*') return;
34
35
  if (typeof schema.preprocessors[key] === 'function') {
35
36
  obj[key] = await Promise.resolve(
36
37
  (schema.preprocessors[key] as (unknown) => unknown)(
37
- key === '*' ? obj : obj[key]
38
+ obj[key]
38
39
  )
39
40
  );
40
41
  } else {
@@ -50,6 +51,24 @@ export const validateObject = async (
50
51
  }
51
52
  })
52
53
  );
54
+ if ('*' in schema.preprocessors) {
55
+ const objectPreprocessor = schema.preprocessors['*'];
56
+ if (typeof objectPreprocessor === 'function') {
57
+ await Promise.resolve(
58
+ (objectPreprocessor as (unknown) => unknown)(obj)
59
+ );
60
+ } else {
61
+ const preprocessor = validator.preprocessors.get(
62
+ objectPreprocessor as string
63
+ );
64
+ if (typeof preprocessor !== 'function') {
65
+ throw new Error(
66
+ `preprocessor '${objectPreprocessor}' is unknown`
67
+ );
68
+ }
69
+ await Promise.resolve(preprocessor(obj));
70
+ }
71
+ }
53
72
  }
54
73
 
55
74
  if (typeof schema.properties === 'object' && schema.properties) {