@oscarpalmer/jhunal 0.3.0 → 0.4.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 (43) hide show
  1. package/README.md +2 -0
  2. package/dist/helpers.cjs +24 -3
  3. package/dist/helpers.js +24 -3
  4. package/dist/is.cjs +19 -0
  5. package/dist/is.js +14 -0
  6. package/dist/schematic.cjs +5 -3
  7. package/dist/schematic.js +5 -3
  8. package/dist/validation/schema.validation.cjs +54 -0
  9. package/dist/validation/schema.validation.js +49 -0
  10. package/dist/validation/type.validation.cjs +33 -0
  11. package/dist/validation/type.validation.js +29 -0
  12. package/dist/validation/value.validation.cjs +35 -0
  13. package/dist/validation/value.validation.js +31 -0
  14. package/package.json +1 -1
  15. package/src/helpers.ts +33 -5
  16. package/src/index.ts +1 -1
  17. package/src/is.ts +22 -0
  18. package/src/model.ts +84 -43
  19. package/src/schematic.ts +14 -10
  20. package/src/validation/schema.validation.ts +63 -0
  21. package/src/validation/type.validation.ts +34 -0
  22. package/src/validation/value.validation.ts +46 -0
  23. package/types/helpers.d.cts +26 -1
  24. package/types/helpers.d.ts +2 -2
  25. package/types/index.d.cts +25 -14
  26. package/types/index.d.ts +1 -1
  27. package/types/is.d.cts +15 -0
  28. package/types/is.d.ts +3 -0
  29. package/types/model.d.cts +27 -12
  30. package/types/model.d.ts +27 -11
  31. package/types/schematic.d.cts +25 -14
  32. package/types/schematic.d.ts +4 -4
  33. package/types/validation/schema.validation.d.cts +55 -0
  34. package/types/validation/schema.validation.d.ts +3 -0
  35. package/types/validation/type.validation.d.cts +43 -0
  36. package/types/validation/type.validation.d.ts +2 -0
  37. package/types/validation/value.validation.d.cts +43 -0
  38. package/types/validation/value.validation.d.ts +2 -0
  39. package/dist/validation.cjs +0 -89
  40. package/dist/validation.js +0 -84
  41. package/src/validation.ts +0 -109
  42. package/types/validation.d.cts +0 -28
  43. package/types/validation.d.ts +0 -3
@@ -0,0 +1,55 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export type Property = {
4
+ required?: boolean;
5
+ type: PropertyType | PropertyType[];
6
+ };
7
+ export type PropertyType = Schema | Schematic<unknown> | ValueKey;
8
+ /**
9
+ * A schema for validating objects
10
+ */
11
+ export type Schema = {
12
+ [key: string]: Property | ValueKey | ValueKey[];
13
+ };
14
+ /**
15
+ * A schematic for validating objects
16
+ */
17
+ export type Schematic<Model> = {
18
+ /**
19
+ * Does the value match the schema?
20
+ */
21
+ is(value: unknown): value is Model;
22
+ };
23
+ export type ValidatedProperty = {
24
+ required: boolean;
25
+ types: ValidatedPropertyType[];
26
+ };
27
+ export type ValidatedPropertyType = Schematic<unknown> | ValidatedSchema | ValueKey;
28
+ export type ValidatedSchema = {
29
+ keys: string[];
30
+ length: number;
31
+ properties: ValidatedSchemaProperties;
32
+ };
33
+ export type ValidatedSchemaProperties = {
34
+ [key: string]: ValidatedProperty;
35
+ };
36
+ export type ValueKey = keyof Values;
37
+ export type Values = {
38
+ array: unknown[];
39
+ bigint: bigint;
40
+ boolean: boolean;
41
+ date: Date;
42
+ "date-like": number | string | Date;
43
+ function: Function;
44
+ null: null;
45
+ number: number;
46
+ numerical: bigint | number;
47
+ object: object;
48
+ string: string;
49
+ symbol: symbol;
50
+ undefined: undefined;
51
+ };
52
+ export declare function validateSchema(schema: unknown): ValidatedSchema;
53
+ export declare const validatedSchemas: WeakMap<Schema, ValidatedSchema>;
54
+
55
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Schema, ValidatedSchema } from '../model';
2
+ export declare function validateSchema(schema: unknown): ValidatedSchema;
3
+ export declare const validatedSchemas: WeakMap<Schema, ValidatedSchema>;
@@ -0,0 +1,43 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ * A schematic for validating objects
5
+ */
6
+ export type Schematic<Model> = {
7
+ /**
8
+ * Does the value match the schema?
9
+ */
10
+ is(value: unknown): value is Model;
11
+ };
12
+ export type ValidatedProperty = {
13
+ required: boolean;
14
+ types: ValidatedPropertyType[];
15
+ };
16
+ export type ValidatedPropertyType = Schematic<unknown> | ValidatedSchema | ValueKey;
17
+ export type ValidatedSchema = {
18
+ keys: string[];
19
+ length: number;
20
+ properties: ValidatedSchemaProperties;
21
+ };
22
+ export type ValidatedSchemaProperties = {
23
+ [key: string]: ValidatedProperty;
24
+ };
25
+ export type ValueKey = keyof Values;
26
+ export type Values = {
27
+ array: unknown[];
28
+ bigint: bigint;
29
+ boolean: boolean;
30
+ date: Date;
31
+ "date-like": number | string | Date;
32
+ function: Function;
33
+ null: null;
34
+ number: number;
35
+ numerical: bigint | number;
36
+ object: object;
37
+ string: string;
38
+ symbol: symbol;
39
+ undefined: undefined;
40
+ };
41
+ export declare function validateType(type: ValidatedPropertyType, value: unknown): boolean;
42
+
43
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { ValidatedPropertyType } from '../model';
2
+ export declare function validateType(type: ValidatedPropertyType, value: unknown): boolean;
@@ -0,0 +1,43 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ * A schematic for validating objects
5
+ */
6
+ export type Schematic<Model> = {
7
+ /**
8
+ * Does the value match the schema?
9
+ */
10
+ is(value: unknown): value is Model;
11
+ };
12
+ export type ValidatedProperty = {
13
+ required: boolean;
14
+ types: ValidatedPropertyType[];
15
+ };
16
+ export type ValidatedPropertyType = Schematic<unknown> | ValidatedSchema | ValueKey;
17
+ export type ValidatedSchema = {
18
+ keys: string[];
19
+ length: number;
20
+ properties: ValidatedSchemaProperties;
21
+ };
22
+ export type ValidatedSchemaProperties = {
23
+ [key: string]: ValidatedProperty;
24
+ };
25
+ export type ValueKey = keyof Values;
26
+ export type Values = {
27
+ array: unknown[];
28
+ bigint: bigint;
29
+ boolean: boolean;
30
+ date: Date;
31
+ "date-like": number | string | Date;
32
+ function: Function;
33
+ null: null;
34
+ number: number;
35
+ numerical: bigint | number;
36
+ object: object;
37
+ string: string;
38
+ symbol: symbol;
39
+ undefined: undefined;
40
+ };
41
+ export declare function validateValue(validated: ValidatedSchema, obj: unknown): boolean;
42
+
43
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { ValidatedSchema } from '../model';
2
+ export declare function validateValue(validated: ValidatedSchema, obj: unknown): boolean;
@@ -1,89 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const helpers = require('./helpers.cjs');
6
-
7
- function getValidatedSchema(schema) {
8
- const validated = {
9
- keys: [],
10
- length: 0,
11
- properties: {}
12
- };
13
- if (typeof schema !== "object" || schema === null) {
14
- return validated;
15
- }
16
- const keys = Object.keys(schema);
17
- const { length } = keys;
18
- for (let index = 0; index < length; index += 1) {
19
- const key = keys[index];
20
- const value = schema[key];
21
- let required = true;
22
- let valueTypes;
23
- if (Array.isArray(value)) {
24
- valueTypes = helpers.getTypes(value);
25
- } else if (typeof value === "object" && value !== null) {
26
- if (typeof value.required === "boolean") {
27
- required = value.required;
28
- }
29
- valueTypes = helpers.getTypes(value.type);
30
- } else {
31
- valueTypes = helpers.getTypes(value);
32
- }
33
- if (valueTypes.length > 0) {
34
- if (!required && !valueTypes.includes("undefined")) {
35
- valueTypes.push("undefined");
36
- }
37
- validated.keys.push(key);
38
- validated.properties[key] = {
39
- required,
40
- types: valueTypes
41
- };
42
- validated.length += 1;
43
- }
44
- }
45
- return validated;
46
- }
47
- function validate(validated, obj) {
48
- if (typeof obj !== "object" || obj === null) {
49
- return false;
50
- }
51
- outer: for (let index = 0; index < validated.length; index += 1) {
52
- const key = validated.keys[index];
53
- const property = validated.properties[key];
54
- const value = obj[key];
55
- if (value === void 0 && property.required && !property.types.includes("undefined")) {
56
- return false;
57
- }
58
- const typesLength = property.types.length;
59
- if (typesLength === 1) {
60
- if (!validators[property.types[0]](value)) {
61
- return false;
62
- }
63
- continue;
64
- }
65
- for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
66
- if (validators[property.types[typeIndex]](value)) {
67
- continue outer;
68
- }
69
- }
70
- return false;
71
- }
72
- return true;
73
- }
74
- const validators = {
75
- array: Array.isArray,
76
- bigint: (value) => typeof value === "bigint",
77
- boolean: (value) => typeof value === "boolean",
78
- date: (value) => value instanceof Date,
79
- function: (value) => typeof value === "function",
80
- null: (value) => value === null,
81
- number: (value) => typeof value === "number",
82
- object: (value) => typeof value === "object" && value !== null,
83
- string: (value) => typeof value === "string",
84
- symbol: (value) => typeof value === "symbol",
85
- undefined: (value) => value === void 0
86
- };
87
-
88
- exports.getValidatedSchema = getValidatedSchema;
89
- exports.validate = validate;
@@ -1,84 +0,0 @@
1
- import { getTypes } from './helpers.js';
2
-
3
- function getValidatedSchema(schema) {
4
- const validated = {
5
- keys: [],
6
- length: 0,
7
- properties: {}
8
- };
9
- if (typeof schema !== "object" || schema === null) {
10
- return validated;
11
- }
12
- const keys = Object.keys(schema);
13
- const { length } = keys;
14
- for (let index = 0; index < length; index += 1) {
15
- const key = keys[index];
16
- const value = schema[key];
17
- let required = true;
18
- let valueTypes;
19
- if (Array.isArray(value)) {
20
- valueTypes = getTypes(value);
21
- } else if (typeof value === "object" && value !== null) {
22
- if (typeof value.required === "boolean") {
23
- required = value.required;
24
- }
25
- valueTypes = getTypes(value.type);
26
- } else {
27
- valueTypes = getTypes(value);
28
- }
29
- if (valueTypes.length > 0) {
30
- if (!required && !valueTypes.includes("undefined")) {
31
- valueTypes.push("undefined");
32
- }
33
- validated.keys.push(key);
34
- validated.properties[key] = {
35
- required,
36
- types: valueTypes
37
- };
38
- validated.length += 1;
39
- }
40
- }
41
- return validated;
42
- }
43
- function validate(validated, obj) {
44
- if (typeof obj !== "object" || obj === null) {
45
- return false;
46
- }
47
- outer: for (let index = 0; index < validated.length; index += 1) {
48
- const key = validated.keys[index];
49
- const property = validated.properties[key];
50
- const value = obj[key];
51
- if (value === void 0 && property.required && !property.types.includes("undefined")) {
52
- return false;
53
- }
54
- const typesLength = property.types.length;
55
- if (typesLength === 1) {
56
- if (!validators[property.types[0]](value)) {
57
- return false;
58
- }
59
- continue;
60
- }
61
- for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
62
- if (validators[property.types[typeIndex]](value)) {
63
- continue outer;
64
- }
65
- }
66
- return false;
67
- }
68
- return true;
69
- }
70
- const validators = {
71
- array: Array.isArray,
72
- bigint: (value) => typeof value === "bigint",
73
- boolean: (value) => typeof value === "boolean",
74
- date: (value) => value instanceof Date,
75
- function: (value) => typeof value === "function",
76
- null: (value) => value === null,
77
- number: (value) => typeof value === "number",
78
- object: (value) => typeof value === "object" && value !== null,
79
- string: (value) => typeof value === "string",
80
- symbol: (value) => typeof value === "symbol",
81
- undefined: (value) => value === void 0
82
- };
83
-
84
- export { getValidatedSchema, validate };
package/src/validation.ts DELETED
@@ -1,109 +0,0 @@
1
- import type {PlainObject} from '@oscarpalmer/atoms';
2
- import {getTypes} from './helpers';
3
- import type {Schema, ValidatedSchema, Values} from './model';
4
-
5
- export function getValidatedSchema(schema: unknown): ValidatedSchema {
6
- const validated: ValidatedSchema = {
7
- keys: [],
8
- length: 0,
9
- properties: {},
10
- };
11
-
12
- if (typeof schema !== 'object' || schema === null) {
13
- return validated;
14
- }
15
-
16
- const keys = Object.keys(schema);
17
- const {length} = keys;
18
-
19
- for (let index = 0; index < length; index += 1) {
20
- const key = keys[index];
21
- const value = (schema as Schema)[key];
22
-
23
- let required = true;
24
- let valueTypes: (keyof Values)[];
25
-
26
- if (Array.isArray(value)) {
27
- valueTypes = getTypes(value);
28
- } else if (typeof value === 'object' && value !== null) {
29
- if (typeof (value as PlainObject).required === 'boolean') {
30
- required = (value as PlainObject).required as boolean;
31
- }
32
-
33
- valueTypes = getTypes((value as PlainObject).type);
34
- } else {
35
- valueTypes = getTypes(value);
36
- }
37
-
38
- if (valueTypes.length > 0) {
39
- if (!required && !valueTypes.includes('undefined')) {
40
- valueTypes.push('undefined');
41
- }
42
-
43
- validated.keys.push(key);
44
-
45
- validated.properties[key] = {
46
- required,
47
- types: valueTypes,
48
- };
49
-
50
- validated.length += 1;
51
- }
52
- }
53
-
54
- return validated;
55
- }
56
-
57
- export function validate(validated: ValidatedSchema, obj: unknown): boolean {
58
- if (typeof obj !== 'object' || obj === null) {
59
- return false;
60
- }
61
-
62
- outer: for (let index = 0; index < validated.length; index += 1) {
63
- const key = validated.keys[index];
64
- const property = validated.properties[key];
65
- const value = (obj as PlainObject)[key];
66
-
67
- if (
68
- value === undefined &&
69
- property.required &&
70
- !property.types.includes('undefined')
71
- ) {
72
- return false;
73
- }
74
-
75
- const typesLength = property.types.length;
76
-
77
- if (typesLength === 1) {
78
- if (!validators[property.types[0]](value)) {
79
- return false;
80
- }
81
-
82
- continue;
83
- }
84
-
85
- for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
86
- if (validators[property.types[typeIndex]](value)) {
87
- continue outer;
88
- }
89
- }
90
-
91
- return false;
92
- }
93
-
94
- return true;
95
- }
96
-
97
- const validators: Record<keyof Values, (value: unknown) => boolean> = {
98
- array: Array.isArray,
99
- bigint: value => typeof value === 'bigint',
100
- boolean: value => typeof value === 'boolean',
101
- date: value => value instanceof Date,
102
- function: value => typeof value === 'function',
103
- null: value => value === null,
104
- number: value => typeof value === 'number',
105
- object: value => typeof value === 'object' && value !== null,
106
- string: value => typeof value === 'string',
107
- symbol: value => typeof value === 'symbol',
108
- undefined: value => value === undefined,
109
- };
@@ -1,28 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export type ValidatedProperty = {
4
- required: boolean;
5
- types: (keyof Values)[];
6
- };
7
- export type ValidatedSchema = {
8
- keys: string[];
9
- length: number;
10
- properties: Record<string, ValidatedProperty>;
11
- };
12
- export type Values = {
13
- array: unknown[];
14
- bigint: bigint;
15
- boolean: boolean;
16
- date: Date;
17
- function: Function;
18
- null: null;
19
- number: number;
20
- object: object;
21
- string: string;
22
- symbol: symbol;
23
- undefined: undefined;
24
- };
25
- export declare function getValidatedSchema(schema: unknown): ValidatedSchema;
26
- export declare function validate(validated: ValidatedSchema, obj: unknown): boolean;
27
-
28
- export {};
@@ -1,3 +0,0 @@
1
- import type { ValidatedSchema } from './model';
2
- export declare function getValidatedSchema(schema: unknown): ValidatedSchema;
3
- export declare function validate(validated: ValidatedSchema, obj: unknown): boolean;