@cleverbrush/schema 1.0.0-beta.5 → 1.1.0

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.
Files changed (76) hide show
  1. package/README.md +118 -320
  2. package/dist/builders/AnySchemaBuilder.d.ts +76 -0
  3. package/dist/builders/AnySchemaBuilder.js +112 -0
  4. package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
  5. package/dist/builders/ArraySchemaBuilder.js +254 -111
  6. package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
  7. package/dist/builders/BooleanSchemaBuilder.js +134 -74
  8. package/dist/builders/DateSchemaBuilder.d.ts +179 -0
  9. package/dist/builders/DateSchemaBuilder.js +433 -0
  10. package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
  11. package/dist/builders/FunctionSchemaBuilder.js +102 -58
  12. package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
  13. package/dist/builders/NumberSchemaBuilder.js +345 -165
  14. package/dist/builders/ObjectSchemaBuilder.d.ts +310 -81
  15. package/dist/builders/ObjectSchemaBuilder.js +528 -283
  16. package/dist/builders/SchemaBuilder.d.ts +157 -34
  17. package/dist/builders/SchemaBuilder.js +258 -71
  18. package/dist/builders/StringSchemaBuilder.d.ts +138 -13
  19. package/dist/builders/StringSchemaBuilder.js +261 -115
  20. package/dist/builders/UnionSchemaBuilder.d.ts +132 -9
  21. package/dist/builders/UnionSchemaBuilder.js +196 -75
  22. package/dist/index.d.ts +20 -40
  23. package/dist/index.js +19 -32
  24. package/dist/utils/transaction.d.ts +46 -0
  25. package/dist/utils/transaction.js +178 -0
  26. package/package.json +27 -14
  27. package/dist/defaultSchemas.d.ts +0 -4
  28. package/dist/defaultSchemas.js +0 -45
  29. package/dist/schema.d.ts +0 -231
  30. package/dist/schema.js +0 -2
  31. package/dist/schemaRegistry.d.ts +0 -49
  32. package/dist/schemaRegistry.js +0 -278
  33. package/dist/validators/validateArray.d.ts +0 -3
  34. package/dist/validators/validateArray.js +0 -60
  35. package/dist/validators/validateBoolean.d.ts +0 -2
  36. package/dist/validators/validateBoolean.js +0 -30
  37. package/dist/validators/validateFunction.d.ts +0 -2
  38. package/dist/validators/validateFunction.js +0 -21
  39. package/dist/validators/validateNumber.d.ts +0 -2
  40. package/dist/validators/validateNumber.js +0 -69
  41. package/dist/validators/validateObject.d.ts +0 -3
  42. package/dist/validators/validateObject.js +0 -95
  43. package/dist/validators/validateString.d.ts +0 -2
  44. package/dist/validators/validateString.js +0 -50
  45. package/dist/validators/validateUnion.d.ts +0 -3
  46. package/dist/validators/validateUnion.js +0 -30
  47. package/src/builders/ArraySchemaBuilder.test.ts +0 -270
  48. package/src/builders/ArraySchemaBuilder.ts +0 -381
  49. package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
  50. package/src/builders/BooleanSchemaBuilder.ts +0 -155
  51. package/src/builders/FunctionSchemaBuilder.test.ts +0 -145
  52. package/src/builders/FunctionSchemaBuilder.ts +0 -117
  53. package/src/builders/NumberSchemaBuilder.test.ts +0 -493
  54. package/src/builders/NumberSchemaBuilder.ts +0 -789
  55. package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
  56. package/src/builders/ObjectSchemaBuilder.ts +0 -794
  57. package/src/builders/SchemaBuilder.test.ts +0 -73
  58. package/src/builders/SchemaBuilder.ts +0 -135
  59. package/src/builders/StringSchemaBuilder.test.ts +0 -318
  60. package/src/builders/StringSchemaBuilder.ts +0 -392
  61. package/src/builders/UnionSchemaBuilder.test.ts +0 -162
  62. package/src/builders/UnionSchemaBuilder.ts +0 -154
  63. package/src/defaultSchemas.ts +0 -44
  64. package/src/index.ts +0 -66
  65. package/src/schema.ts +0 -849
  66. package/src/schemaRegistry.builders.test.ts +0 -1393
  67. package/src/schemaRegistry.test.ts +0 -118
  68. package/src/schemaRegistry.ts +0 -461
  69. package/src/validators/validateArray.ts +0 -76
  70. package/src/validators/validateBoolean.ts +0 -36
  71. package/src/validators/validateFunction.ts +0 -25
  72. package/src/validators/validateNumber.ts +0 -82
  73. package/src/validators/validateObject.ts +0 -119
  74. package/src/validators/validateString.ts +0 -59
  75. package/src/validators/validateUnion.ts +0 -36
  76. package/tsconfig.json +0 -16
@@ -1,25 +0,0 @@
1
- import { FunctionSchema, ValidationResult } from '../schema.js';
2
-
3
- export const validateFunction = async (
4
- obj: any,
5
- schema: FunctionSchema
6
- ): Promise<ValidationResult> => {
7
- if (
8
- typeof obj === 'undefined' &&
9
- typeof schema === 'object' &&
10
- schema.isRequired === false
11
- ) {
12
- return {
13
- valid: true
14
- };
15
- }
16
- if (typeof obj !== 'function')
17
- return {
18
- valid: false,
19
- errors: [`expected type function, but saw ${typeof obj}`]
20
- };
21
-
22
- return {
23
- valid: true
24
- };
25
- };
@@ -1,82 +0,0 @@
1
- import { NumberSchema, ValidationResult } from '../schema.js';
2
-
3
- export const validateNumber = async (
4
- obj: any,
5
- schema: NumberSchema
6
- ): Promise<ValidationResult> => {
7
- if (
8
- typeof obj === 'undefined' &&
9
- typeof schema === 'object' &&
10
- schema.isRequired === false
11
- ) {
12
- return {
13
- valid: true
14
- };
15
- }
16
- if (typeof obj !== 'number')
17
- return {
18
- valid: false,
19
- errors: [`expected type number, but saw ${typeof obj}`]
20
- };
21
-
22
- if (typeof schema === 'number') {
23
- return obj === schema
24
- ? { valid: true }
25
- : {
26
- valid: false,
27
- errors: [`should be equal to ${schema}`]
28
- };
29
- }
30
-
31
- const num = obj as number;
32
-
33
- if (schema.ensureNotNaN && Number.isNaN(num)) {
34
- return {
35
- valid: false,
36
- errors: ['is not expected to be NaN']
37
- };
38
- }
39
-
40
- if (
41
- schema.ensureIsFinite &&
42
- !Number.isFinite(num) &&
43
- schema.ensureNotNaN &&
44
- !Number.isNaN(num)
45
- ) {
46
- return {
47
- valid: false,
48
- errors: ['is expected to be a finite number']
49
- };
50
- }
51
-
52
- if (typeof schema.equals !== 'undefined' && num !== schema.equals) {
53
- return {
54
- valid: false,
55
- errors: [`expected to be equal to ${schema.equals}`]
56
- };
57
- }
58
-
59
- if (typeof schema.min !== 'undefined') {
60
- if (typeof schema.min !== 'number')
61
- throw new Error('min constraint should be a number');
62
- if (num < schema.min)
63
- return {
64
- valid: false,
65
- errors: [`expected to be at least ${schema.min}`]
66
- };
67
- }
68
-
69
- if (typeof schema.max !== 'undefined') {
70
- if (typeof schema.max !== 'number')
71
- throw new Error('max constraint should be a number');
72
- if (num > schema.max)
73
- return {
74
- valid: false,
75
- errors: [`expected to be no more than ${schema.max}`]
76
- };
77
- }
78
-
79
- return {
80
- valid: true
81
- };
82
- };
@@ -1,119 +0,0 @@
1
- import { ObjectSchema, Schema, ValidationResult } from '../schema.js';
2
- import SchemaRegistry from '../schemaRegistry.js';
3
-
4
- export const validateObject = async (
5
- obj: any,
6
- schema: ObjectSchema<any>,
7
- validator: SchemaRegistry<any>
8
- ): Promise<ValidationResult> => {
9
- if (
10
- typeof obj === 'undefined' &&
11
- typeof schema === 'object' &&
12
- schema.isRequired === false
13
- ) {
14
- return {
15
- valid: true
16
- };
17
- }
18
- if (typeof obj !== 'object') {
19
- return {
20
- valid: false,
21
- errors: [
22
- `expected to have type='object', but saw '${typeof obj}' instead`
23
- ]
24
- };
25
- }
26
-
27
- if (typeof schema.preprocessors === 'object' && schema.preprocessors) {
28
- await Promise.all(
29
- Object.keys(schema.preprocessors).map(async (key: string) => {
30
- if (key === '*') return;
31
- if (typeof schema.preprocessors[key] === 'function') {
32
- const res = await Promise.resolve(
33
- (schema.preprocessors[key] as any)(obj[key])
34
- );
35
- if (typeof res !== 'undefined') {
36
- obj[key] = res;
37
- } else {
38
- delete obj[key];
39
- }
40
- } else {
41
- const preprocessor = validator.preprocessors.get(
42
- schema.preprocessors[key] as string
43
- );
44
- if (typeof preprocessor !== 'function') {
45
- throw new Error(
46
- `preprocessor '${schema.preprocessors[key]}' is unknown`
47
- );
48
- }
49
- const res = await Promise.resolve(preprocessor(obj[key]));
50
- if (typeof res !== 'undefined') {
51
- obj[key] = res;
52
- } else {
53
- delete obj[key];
54
- }
55
- }
56
- })
57
- );
58
- if ('*' in schema.preprocessors) {
59
- const objectPreprocessor = schema.preprocessors['*'];
60
- if (typeof objectPreprocessor === 'function') {
61
- await Promise.resolve((objectPreprocessor as any)(obj));
62
- } else {
63
- const preprocessor = validator.preprocessors.get(
64
- objectPreprocessor as string
65
- );
66
- if (typeof preprocessor !== 'function') {
67
- throw new Error(
68
- `preprocessor '${objectPreprocessor}' is unknown`
69
- );
70
- }
71
- await Promise.resolve(preprocessor(obj));
72
- }
73
- }
74
- }
75
-
76
- if (typeof schema.properties === 'object' && schema.properties) {
77
- const errors = [
78
- ...(await Promise.all(
79
- Object.entries(schema.properties).map(
80
- async ([name, schema]: [string, Schema]) => {
81
- const result = await validator.validate(
82
- schema,
83
- obj[name]
84
- );
85
- if (result.valid) return result;
86
- return {
87
- valid: false,
88
- errors: (result.errors || []).map(
89
- (e) => `->${name} ${e}`
90
- )
91
- };
92
- }
93
- )
94
- )),
95
- ...(schema.noUnknownProperties === false
96
- ? []
97
- : Object.keys(obj)
98
- .filter((k) => !(k in schema.properties))
99
- .map((k) => ({
100
- valid: false,
101
- errors: [`-> ${k} - unknown field`]
102
- })))
103
- ]
104
- .filter((r) => !r.valid)
105
- .map((r) => r.errors)
106
- .flat(Infinity) as string[];
107
-
108
- if (errors.length) {
109
- return {
110
- valid: false,
111
- errors
112
- };
113
- }
114
- }
115
-
116
- return {
117
- valid: true
118
- };
119
- };
@@ -1,59 +0,0 @@
1
- import { StringSchema, ValidationResult } from '../schema.js';
2
-
3
- export const validateString = async (
4
- obj: any,
5
- schema: StringSchema
6
- ): Promise<ValidationResult> => {
7
- if (
8
- typeof obj === 'undefined' &&
9
- typeof schema === 'object' &&
10
- schema.isRequired === false
11
- ) {
12
- return {
13
- valid: true
14
- };
15
- }
16
- if (typeof obj !== 'string')
17
- return {
18
- valid: false,
19
- errors: [`expected type string, but saw ${typeof obj}`]
20
- };
21
-
22
- if (typeof schema === 'string') {
23
- return obj === schema
24
- ? { valid: true }
25
- : {
26
- valid: false,
27
- errors: [`should be equal to ${schema}`]
28
- };
29
- }
30
-
31
- const str = obj as string;
32
-
33
- if (typeof schema.equals === 'string' && str !== schema.equals) {
34
- return {
35
- valid: false,
36
- errors: [
37
- `expected to be equal to '${schema.equals}' but saw '${str}'`
38
- ]
39
- };
40
- }
41
-
42
- if (typeof schema.minLength === 'number' && str.length < schema.minLength) {
43
- return {
44
- valid: false,
45
- errors: [`expected to be at least ${schema.minLength} chars long`]
46
- };
47
- }
48
-
49
- if (typeof schema.maxLength === 'number' && str.length > schema.maxLength) {
50
- return {
51
- valid: false,
52
- errors: [`expected to be at most ${schema.maxLength} chars long`]
53
- };
54
- }
55
-
56
- return {
57
- valid: true
58
- };
59
- };
@@ -1,36 +0,0 @@
1
- import { UnionSchema, ValidationResult } from '../schema.js';
2
- import SchemaRegistry from '../schemaRegistry.js';
3
-
4
- export const validateUnion = async (
5
- obj: any,
6
- schema: UnionSchema<any>,
7
- validator: SchemaRegistry<any>
8
- ): Promise<ValidationResult> => {
9
- if (typeof obj === 'undefined' && schema.isRequired === false) {
10
- return {
11
- valid: true
12
- };
13
- }
14
-
15
- if (obj === null && schema.isNullable) {
16
- return {
17
- valid: true
18
- };
19
- }
20
-
21
- if (Array.isArray(schema.variants) && schema.variants.length > 0) {
22
- for (let i = 0; i < schema.variants.length; i++) {
23
- const variant = schema.variants[i];
24
- const result = await validator.validate(variant, obj);
25
- if (result.valid) {
26
- return result;
27
- }
28
- }
29
- return {
30
- valid: false,
31
- errors: ['object does not satisfy any scheme']
32
- };
33
- }
34
-
35
- throw new Error('variants should be non empty array');
36
- };
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "rootDir": "./src",
4
- "outDir": "./dist",
5
- "declaration": true,
6
- "target": "esnext",
7
- "module": "CommonJS",
8
- "esModuleInterop": true,
9
- "allowSyntheticDefaultImports": true,
10
- "moduleResolution": "Node"
11
- },
12
- "files": ["./src/index.ts"],
13
- "watchOptions": {
14
- "excludeDirectories": ["./dist"]
15
- }
16
- }