validno 0.4.4 → 0.4.5

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 (42) hide show
  1. package/dist/dev.d.ts +6 -1
  2. package/dist/dev.d.ts.map +1 -1
  3. package/dist/dev.js +116 -5
  4. package/dist/engine/methods/validateType.d.ts.map +1 -1
  5. package/dist/engine/methods/validateType.js +4 -0
  6. package/package.json +1 -1
  7. package/dist/tests/cases/corruptedData.test.d.ts +0 -2
  8. package/dist/tests/cases/corruptedData.test.d.ts.map +0 -1
  9. package/dist/tests/cases/corruptedData.test.js +0 -81
  10. package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts +0 -2
  11. package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts.map +0 -1
  12. package/dist/tests/cases/dataInputIsNotAnObject.test.js +0 -24
  13. package/dist/tests/cases/notRequiredNestedKey.test.d.ts +0 -2
  14. package/dist/tests/cases/notRequiredNestedKey.test.d.ts.map +0 -1
  15. package/dist/tests/cases/notRequiredNestedKey.test.js +0 -38
  16. package/dist/tests/cases/secondLevelDeepValidates.test.d.ts +0 -2
  17. package/dist/tests/cases/secondLevelDeepValidates.test.d.ts.map +0 -1
  18. package/dist/tests/cases/secondLevelDeepValidates.test.js +0 -112
  19. package/dist/tests/customMessage.test.d.ts +0 -2
  20. package/dist/tests/customMessage.test.d.ts.map +0 -1
  21. package/dist/tests/customMessage.test.js +0 -102
  22. package/dist/tests/customType.test.d.ts +0 -2
  23. package/dist/tests/customType.test.d.ts.map +0 -1
  24. package/dist/tests/customType.test.js +0 -146
  25. package/dist/tests/joinErrors.test.d.ts +0 -2
  26. package/dist/tests/joinErrors.test.d.ts.map +0 -1
  27. package/dist/tests/joinErrors.test.js +0 -62
  28. package/dist/tests/missing.test.d.ts +0 -2
  29. package/dist/tests/missing.test.d.ts.map +0 -1
  30. package/dist/tests/missing.test.js +0 -224
  31. package/dist/tests/onlyKeys.test.d.ts +0 -2
  32. package/dist/tests/onlyKeys.test.d.ts.map +0 -1
  33. package/dist/tests/onlyKeys.test.js +0 -74
  34. package/dist/tests/types.test.d.ts +0 -2
  35. package/dist/tests/types.test.d.ts.map +0 -1
  36. package/dist/tests/types.test.js +0 -273
  37. package/dist/tests/utils/_errors.test.d.ts +0 -2
  38. package/dist/tests/utils/_errors.test.d.ts.map +0 -1
  39. package/dist/tests/utils/_errors.test.js +0 -47
  40. package/dist/tests/utils/_validations.test.d.ts +0 -2
  41. package/dist/tests/utils/_validations.test.d.ts.map +0 -1
  42. package/dist/tests/utils/_validations.test.js +0 -773
package/dist/dev.d.ts CHANGED
@@ -1,2 +1,7 @@
1
- export {};
1
+ export declare const AccessScopes: {
2
+ readonly Inherit: "inherit";
3
+ readonly Public: "public";
4
+ readonly Project: "workspace";
5
+ readonly User: "user";
6
+ };
2
7
  //# sourceMappingURL=dev.d.ts.map
package/dist/dev.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,YAAY;;;;;CAKf,CAAC"}
package/dist/dev.js CHANGED
@@ -1,10 +1,121 @@
1
1
  import { Schema } from "./Schema.js";
2
+ import isObjectId from "./utils/isObjectId.js";
3
+ import { ObjectId } from "mongodb";
4
+ export const AccessScopes = {
5
+ Inherit: 'inherit',
6
+ Public: 'public',
7
+ Project: 'workspace',
8
+ User: 'user'
9
+ };
10
+ const checkMethodScope = (value, context) => {
11
+ const authCollection = context.input.authCollection;
12
+ if (isObjectId(authCollection, ObjectId))
13
+ return true;
14
+ const mainScope = context.input.scope;
15
+ const methods = context.input.methods;
16
+ const allMethodsScopes = [
17
+ mainScope,
18
+ ...Object.values(methods).filter(method => method.isActive).map(method => method.scope)
19
+ ];
20
+ if (!allMethodsScopes.includes(AccessScopes.User) && !allMethodsScopes.includes(AccessScopes.Project)) {
21
+ return true;
22
+ }
23
+ return {
24
+ result: false,
25
+ details: 'Invalid scope. To use User or Project scope, the collection must have authCollection to be set.'
26
+ };
27
+ };
2
28
  const test = () => {
3
- console.log('--- Custom Test ---');
4
- const schema = new Schema({
5
- anyKey: { type: String }
29
+ const data = {
30
+ scope: 'public',
31
+ methods: {
32
+ get: { isActive: false, scope: 'public' },
33
+ getAll: {
34
+ isActive: true,
35
+ scope: 'public',
36
+ sort: [Object],
37
+ filter: [Object],
38
+ search: [Object]
39
+ },
40
+ create: { isActive: true, scope: 'public' },
41
+ update: { isActive: false, scope: 'public', allowedFields: [] },
42
+ delete: { isActive: false, scope: 'public' },
43
+ distinct: { isActive: false, scope: 'public', fields: [] }
44
+ },
45
+ authSettings: null,
46
+ authCollection: null
47
+ };
48
+ const collectionSettingsSchema = new Schema({
49
+ authCollection: {
50
+ type: [ObjectId, null],
51
+ required: false,
52
+ },
53
+ scope: {
54
+ type: String,
55
+ rules: {
56
+ enum: Object.values(AccessScopes).filter(scope => scope !== AccessScopes.Inherit),
57
+ custom: checkMethodScope
58
+ },
59
+ },
60
+ methods: {
61
+ get: {
62
+ isActive: { type: Boolean },
63
+ scope: {
64
+ type: String,
65
+ rules: { enum: Object.values(AccessScopes) },
66
+ required: false
67
+ }
68
+ },
69
+ getAll: {
70
+ isActive: { type: Boolean },
71
+ scope: {
72
+ type: String,
73
+ rules: {
74
+ enum: Object.values(AccessScopes)
75
+ },
76
+ required: false
77
+ },
78
+ sort: { default: { type: String, required: false } },
79
+ filter: { fields: { type: Array, eachType: String, required: false } },
80
+ search: { fields: { type: Array, eachType: String, required: false } }
81
+ },
82
+ create: {
83
+ isActive: { type: Boolean },
84
+ scope: {
85
+ type: String,
86
+ rules: { enum: Object.values(AccessScopes) },
87
+ required: false
88
+ }
89
+ },
90
+ update: {
91
+ isActive: { type: Boolean },
92
+ scope: {
93
+ type: String,
94
+ rules: { enum: Object.values(AccessScopes) },
95
+ required: false
96
+ },
97
+ allowedFields: { type: Array, eachType: String, required: false }
98
+ },
99
+ delete: {
100
+ isActive: { type: Boolean },
101
+ scope: {
102
+ type: String,
103
+ rules: { enum: Object.values(AccessScopes) },
104
+ required: false
105
+ }
106
+ },
107
+ distinct: {
108
+ isActive: { type: Boolean },
109
+ scope: {
110
+ type: String,
111
+ rules: { enum: Object.values(AccessScopes) },
112
+ required: false
113
+ },
114
+ fields: { type: Array, eachType: String }
115
+ }
116
+ }
6
117
  });
7
- const result = schema.validate({ anyKey: null });
8
- console.log(result);
118
+ const validateResult = collectionSettingsSchema.validate(data);
119
+ console.log(validateResult);
9
120
  };
10
121
  test();
@@ -1 +1 @@
1
- {"version":3,"file":"validateType.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,uBAAuB,CAAC;AACtE,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAiLhD,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,gBAAgB,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,OAAO,EAAE,CAAA;CACzB;AAED,iBAAS,YAAY,CAAC,KAAK,EAAE,kBAAkB,QAU9C;AAED,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"validateType.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,uBAAuB,CAAC;AACtE,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAqLhD,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,gBAAgB,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,OAAO,EAAE,CAAA;CACzB;AAED,iBAAS,YAAY,CAAC,KAAK,EAAE,kBAAkB,QAU9C;AAED,eAAe,YAAY,CAAC"}
@@ -115,6 +115,10 @@ const handleTypeValidation = (key, value, requirements, keyName = key) => {
115
115
  break;
116
116
  }
117
117
  default: {
118
+ if (value === null && typeBySchema !== null) {
119
+ result.push(_validateType.getResult(keyName, false, getDetails(false)));
120
+ break;
121
+ }
118
122
  const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
119
123
  const isConstructorSame = typeof typeBySchema === 'function' && ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
120
124
  const isBothObjectId = isObjectId(value, typeBySchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "description": "A lightweight TypeScript validation library for runtime data validation. Used as part of Kodzero API framework.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=corruptedData.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"corruptedData.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/corruptedData.test.ts"],"names":[],"mappings":""}
@@ -1,81 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../../Schema';
3
- const AccessScopes = {
4
- Public: 'public',
5
- Project: 'workspace',
6
- User: 'user'
7
- };
8
- const collectionSettingsSchema = new Schema({
9
- auth: { type: null },
10
- scope: {
11
- type: String,
12
- rules: {
13
- enum: Object.values(AccessScopes)
14
- },
15
- },
16
- methods: {
17
- get: {
18
- isActive: { type: Boolean },
19
- scope: {
20
- type: String,
21
- rules: { enum: Object.values(AccessScopes) },
22
- required: false
23
- }
24
- },
25
- getAll: {
26
- isActive: { type: Boolean },
27
- scope: {
28
- type: String,
29
- rules: {
30
- enum: Object.values(AccessScopes)
31
- },
32
- required: false
33
- },
34
- sort: { default: { type: String } },
35
- filter: { fields: { type: Array, eachType: String } },
36
- search: { fields: { type: Array, eachType: String } }
37
- },
38
- create: {
39
- isActive: { type: Boolean },
40
- scope: {
41
- type: String,
42
- rules: { enum: Object.values(AccessScopes) },
43
- required: false
44
- }
45
- },
46
- update: {
47
- isActive: { type: Boolean },
48
- scope: {
49
- type: String,
50
- rules: { enum: Object.values(AccessScopes) },
51
- required: false
52
- },
53
- allowedFields: { type: Array, eachType: String }
54
- },
55
- delete: {
56
- isActive: { type: Boolean },
57
- scope: {
58
- type: String,
59
- rules: { enum: Object.values(AccessScopes) },
60
- required: false
61
- }
62
- },
63
- distinct: {
64
- isActive: { type: Boolean },
65
- scope: {
66
- type: String,
67
- rules: { enum: Object.values(AccessScopes) },
68
- required: false
69
- },
70
- fields: { type: Array, eachType: String }
71
- }
72
- }
73
- });
74
- const settingsData = { test: 1 };
75
- describe('Corrupted data in nested keys', () => {
76
- test('Corrupted data in nested keys should not pass the validation', () => {
77
- const result = collectionSettingsSchema.validate(settingsData);
78
- expect(result.ok).toBe(false);
79
- expect(result.errors.length).toBeGreaterThan(1);
80
- });
81
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=dataInputIsNotAnObject.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dataInputIsNotAnObject.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/dataInputIsNotAnObject.test.ts"],"names":[],"mappings":""}
@@ -1,24 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../../Schema';
3
- describe('Data input is not an object → returns failed validation result', () => {
4
- const schema = new Schema({
5
- name: {
6
- type: String
7
- }
8
- });
9
- test('null input fails validation but not throws', () => {
10
- const result = schema.validate(null);
11
- expect(result).toBeDefined();
12
- expect(result.ok).toBe(false);
13
- });
14
- test('array input fails validation but not throws', () => {
15
- const result = schema.validate([]);
16
- expect(result).toBeDefined();
17
- expect(result.ok).toBe(false);
18
- });
19
- test('undefined input fails validation but not throws', () => {
20
- const result = schema.validate(undefined);
21
- expect(result).toBeDefined();
22
- expect(result.ok).toBe(false);
23
- });
24
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=notRequiredNestedKey.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"notRequiredNestedKey.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/notRequiredNestedKey.test.ts"],"names":[],"mappings":""}
@@ -1,38 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../../Schema';
3
- describe('Not required nested key', () => {
4
- test('[1] Nested key that is not required should pass the validation', () => {
5
- const schema = new Schema({
6
- query: { type: Object },
7
- data: { type: Object },
8
- params: {
9
- upsert: { type: Boolean, required: false },
10
- }
11
- });
12
- const data = {
13
- query: {},
14
- data: {}
15
- };
16
- const result = schema.validate(data);
17
- expect(result.ok).toBe(true);
18
- expect(result.missed).toEqual([]);
19
- });
20
- test('[2] Nested key that is required should not pass the validation', () => {
21
- const schema = new Schema({
22
- query: { type: Object },
23
- data: { type: Object },
24
- params: {
25
- upsert: { type: Boolean, required: true },
26
- }
27
- });
28
- const data = {
29
- query: {},
30
- data: {}
31
- };
32
- const result = schema.validate(data);
33
- expect(result.ok).toBe(false);
34
- expect(result.missed).toEqual(['params.upsert']);
35
- expect(result.failed).toEqual(['params.upsert', 'params']);
36
- expect(result.passed).toEqual(['query', 'data']);
37
- });
38
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=secondLevelDeepValidates.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"secondLevelDeepValidates.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/secondLevelDeepValidates.test.ts"],"names":[],"mappings":""}
@@ -1,112 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../../Schema';
3
- const configSchema = new Schema({
4
- app: {
5
- name: {
6
- type: String,
7
- required: true,
8
- rules: { lengthMin: 1, lengthMax: 50 }
9
- },
10
- version: {
11
- type: String,
12
- required: true,
13
- rules: {
14
- regex: /^\d+\.\d+\.\d+$/
15
- }
16
- },
17
- environment: {
18
- type: String,
19
- required: true,
20
- rules: {
21
- enum: ['development', 'testing', 'staging', 'production']
22
- }
23
- }
24
- },
25
- database: {
26
- host: {
27
- type: String,
28
- required: true,
29
- rules: { lengthMin: 1 }
30
- },
31
- port: {
32
- type: Number,
33
- required: true,
34
- rules: { min: 1, max: 65535 }
35
- },
36
- name: {
37
- type: String,
38
- required: true,
39
- rules: { lengthMin: 1, lengthMax: 64 }
40
- },
41
- ssl: {
42
- type: Boolean,
43
- required: false
44
- }
45
- },
46
- api: {
47
- port: {
48
- type: Number,
49
- required: true,
50
- rules: { min: 1000, max: 9999 }
51
- },
52
- cors: {
53
- enabled: { type: Boolean, required: true },
54
- origins: {
55
- type: Array,
56
- eachType: String,
57
- required: false
58
- }
59
- },
60
- rateLimit: {
61
- enabled: { type: Boolean, required: true },
62
- maxRequests: {
63
- type: Number,
64
- required: false,
65
- rules: { min: 1, max: 10000 }
66
- },
67
- windowMs: {
68
- type: Number,
69
- required: false,
70
- rules: { min: 1000, max: 86400000 }
71
- }
72
- }
73
- }
74
- });
75
- const exampleConfigFile = {
76
- "app": {
77
- "name": "MyApp",
78
- "version": "1.0.0",
79
- "environment": "development"
80
- },
81
- "database": {
82
- "host": "localhost",
83
- "port": 5432,
84
- "name": "myappdb",
85
- "ssl": false
86
- },
87
- "api": {
88
- "port": 3000,
89
- "cors": {
90
- "enabled": true,
91
- "origins": ["http://localhost:3000"]
92
- },
93
- "rateLimit": {
94
- "enabled": true,
95
- "maxRequests": 1000,
96
- "windowMs": 60000
97
- }
98
- },
99
- };
100
- describe('Second level deep validates as expected', () => {
101
- test('should validate the api key', () => {
102
- const res = configSchema.validate(exampleConfigFile);
103
- expect(res.ok).toBe(true);
104
- expect(res.failed).toEqual([]);
105
- expect(res.missed).toEqual([]);
106
- });
107
- test('api key shouldn\'t validate with wrong port type', () => {
108
- const res2 = configSchema.validate(Object.assign(Object.assign({}, exampleConfigFile), { api: Object.assign(Object.assign({}, exampleConfigFile.api), { port: '99999' }) }));
109
- expect(res2.ok).toBe(false);
110
- expect(res2.failed).toContain('api');
111
- });
112
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=customMessage.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"customMessage.test.d.ts","sourceRoot":"","sources":["../../src/tests/customMessage.test.ts"],"names":[],"mappings":""}
@@ -1,102 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../Schema';
3
- describe('Тестирование кастомного сообщения об ошибке', () => {
4
- test('Кастомные сообщения корректно отображаются в результате валидации', () => {
5
- const wrongTypeKey = 'wrongType';
6
- const missingKey = 'missingKey';
7
- const wrongRule1 = 'wrongRule1';
8
- const wrongRule2 = 'wrongRule2';
9
- const wrongRule3 = 'wrongRule3';
10
- const allKeys = [
11
- wrongTypeKey,
12
- missingKey,
13
- wrongRule1,
14
- wrongRule2,
15
- wrongRule3
16
- ];
17
- const getCustomMsg = (key, variation = '') => key + ' custom' + variation;
18
- const schema = new Schema({
19
- [wrongTypeKey]: {
20
- type: Number,
21
- required: true,
22
- title: 'Не по типу',
23
- customMessage: (input) => {
24
- return getCustomMsg(wrongTypeKey);
25
- }
26
- },
27
- [missingKey]: {
28
- type: Date,
29
- required: true,
30
- title: 'Отсутствующий',
31
- customMessage: (input) => {
32
- return getCustomMsg(missingKey);
33
- }
34
- },
35
- [wrongRule1]: {
36
- type: String,
37
- required: true,
38
- title: 'Wrong Rule #1',
39
- rules: {
40
- is: 'xxx'
41
- },
42
- customMessage: (input) => {
43
- return getCustomMsg(wrongRule1);
44
- }
45
- },
46
- [wrongRule2]: {
47
- type: String,
48
- required: true,
49
- title: 'Wrong Rule #2',
50
- rules: {
51
- lengthMin: 999
52
- },
53
- customMessage: (input) => {
54
- return getCustomMsg(wrongRule2);
55
- }
56
- },
57
- [wrongRule3]: {
58
- type: String,
59
- required: true,
60
- title: 'Wrong Rule #3',
61
- rules: {
62
- lengthMax: 1
63
- },
64
- customMessage: (input) => {
65
- const { keyword, value, key, title, reqs, schema, rules } = input;
66
- if (keyword === 'lengthMax') {
67
- return getCustomMsg(wrongRule3);
68
- }
69
- return wrongRule3 + ' should reach here';
70
- }
71
- },
72
- });
73
- const obj = {
74
- [wrongTypeKey]: 'abc',
75
- [wrongRule1]: 'def',
76
- [wrongRule2]: 'ghi',
77
- [wrongRule3]: 'jkl'
78
- };
79
- const result = schema.validate(obj);
80
- expect(result).toEqual({
81
- ok: false,
82
- missed: [missingKey],
83
- failed: [...allKeys],
84
- passed: [],
85
- errors: allKeys.map(el => getCustomMsg(el)),
86
- byKeys: {
87
- wrongType: false,
88
- missingKey: false,
89
- wrongRule1: false,
90
- wrongRule2: false,
91
- wrongRule3: false
92
- },
93
- errorsByKeys: {
94
- wrongType: [getCustomMsg(allKeys[0])],
95
- missingKey: [getCustomMsg(allKeys[1])],
96
- wrongRule1: [getCustomMsg(allKeys[2])],
97
- wrongRule2: [getCustomMsg(allKeys[3])],
98
- wrongRule3: [getCustomMsg(allKeys[4])],
99
- }
100
- });
101
- });
102
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=customType.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"customType.test.d.ts","sourceRoot":"","sources":["../../src/tests/customType.test.ts"],"names":[],"mappings":""}