@based/schema 0.0.1

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 (63) hide show
  1. package/README.md +21 -0
  2. package/dist/deepPartial.d.ts +0 -0
  3. package/dist/deepPartial.js +3 -0
  4. package/dist/deepPartial.js.map +1 -0
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.js +20 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/languages.d.ts +187 -0
  9. package/dist/languages.js +190 -0
  10. package/dist/languages.js.map +1 -0
  11. package/dist/schema.d.ts +1 -0
  12. package/dist/schema.js +3 -0
  13. package/dist/schema.js.map +1 -0
  14. package/dist/set/enum.d.ts +2 -0
  15. package/dist/set/enum.js +15 -0
  16. package/dist/set/enum.js.map +1 -0
  17. package/dist/set/fieldValidator.d.ts +6 -0
  18. package/dist/set/fieldValidator.js +144 -0
  19. package/dist/set/fieldValidator.js.map +1 -0
  20. package/dist/set/handleError.d.ts +1 -0
  21. package/dist/set/handleError.js +9 -0
  22. package/dist/set/handleError.js.map +1 -0
  23. package/dist/set/index.d.ts +5 -0
  24. package/dist/set/index.js +93 -0
  25. package/dist/set/index.js.map +1 -0
  26. package/dist/set/parsers.d.ts +6 -0
  27. package/dist/set/parsers.js +287 -0
  28. package/dist/set/parsers.js.map +1 -0
  29. package/dist/setWalker.d.ts +11 -0
  30. package/dist/setWalker.js +189 -0
  31. package/dist/setWalker.js.map +1 -0
  32. package/dist/transformers.d.ts +3 -0
  33. package/dist/transformers.js +18 -0
  34. package/dist/transformers.js.map +1 -0
  35. package/dist/typeWalker.d.ts +3 -0
  36. package/dist/typeWalker.js +18 -0
  37. package/dist/typeWalker.js.map +1 -0
  38. package/dist/types.d.ts +163 -0
  39. package/dist/types.js +8 -0
  40. package/dist/types.js.map +1 -0
  41. package/dist/validate.d.ts +4 -0
  42. package/dist/validate.js +34 -0
  43. package/dist/validate.js.map +1 -0
  44. package/dist/validateFields.d.ts +4 -0
  45. package/dist/validateFields.js +34 -0
  46. package/dist/validateFields.js.map +1 -0
  47. package/dist/validateSchema copy.d.ts +4 -0
  48. package/dist/validateSchema copy.js +34 -0
  49. package/dist/validateSchema copy.js.map +1 -0
  50. package/dist/validateSchema.d.ts +4 -0
  51. package/dist/validateSchema.js +34 -0
  52. package/dist/validateSchema.js.map +1 -0
  53. package/package.json +35 -0
  54. package/src/index.ts +5 -0
  55. package/src/languages.ts +188 -0
  56. package/src/set/handleError.ts +15 -0
  57. package/src/set/index.ts +135 -0
  58. package/src/set/parsers.ts +448 -0
  59. package/src/types.ts +277 -0
  60. package/src/validateSchema.ts +56 -0
  61. package/test/setWalker.ts +197 -0
  62. package/test/validateSchema.ts +42 -0
  63. package/tsconfig.json +9 -0
@@ -0,0 +1,3 @@
1
+ import { BasedSchemaPartial, BasedSchemaTypePartial } from './types';
2
+ export declare const transformField: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
3
+ export declare const transformType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformType = exports.transformField = void 0;
4
+ const transformField = (fromSchema, typeName, type) => {
5
+ if (type.prefix &&
6
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
7
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
8
+ }
9
+ };
10
+ exports.transformField = transformField;
11
+ const transformType = (fromSchema, typeName, type) => {
12
+ if (type.prefix &&
13
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
14
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
15
+ }
16
+ };
17
+ exports.transformType = transformType;
18
+ //# sourceMappingURL=typeWalker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeWalker.js","sourceRoot":"","sources":["../src/typeWalker.ts"],"names":[],"mappings":";;;AAOO,MAAM,cAAc,GAAG,CAC5B,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,cAAc,kBAa1B;AAEM,MAAM,aAAa,GAAG,CAC3B,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,aAAa,iBAazB"}
@@ -0,0 +1,163 @@
1
+ import type { Language } from './languages';
2
+ import type { PartialDeep } from 'type-fest';
3
+ export type AllowedTypes = (string | {
4
+ type?: string;
5
+ $filter: any | any[];
6
+ })[];
7
+ export type BasedSchemaFieldType = 'array' | 'object' | 'record' | 'set' | 'string' | 'number' | 'float' | 'json' | 'integer' | 'timestamp' | 'reference' | 'references' | 'text';
8
+ export declare const isCollection: (type: string) => boolean;
9
+ export type BasedSchemaPattern = string;
10
+ export type BasedSchemaLanguage = Language;
11
+ export type BasedSchemaTypePrefix = string;
12
+ export type BasedSchemaContentMediaType = 'text/html' | 'text/plain' | 'text/markdown' | 'image/png' | 'image/jpeg' | 'video/mp4' | string;
13
+ export type BasedSchemaFieldShared = {
14
+ $id?: string;
15
+ $schema?: string;
16
+ isRequired?: boolean;
17
+ title?: string;
18
+ description?: string;
19
+ readOnly?: boolean;
20
+ writeOnly?: boolean;
21
+ $comment?: string;
22
+ examples?: any[];
23
+ default?: any;
24
+ customValidator?: (value: any, path: (number | string)[], target: BasedSetTarget) => Promise<boolean>;
25
+ $defs?: {
26
+ [key: string]: BasedSchemaField;
27
+ };
28
+ };
29
+ export type BasedSchemaFieldString = {
30
+ type: 'string';
31
+ minLength?: number;
32
+ maxLength?: number;
33
+ contentMediaEncoding?: string;
34
+ contentMediaType?: BasedSchemaContentMediaType;
35
+ pattern?: BasedSchemaPattern;
36
+ format?: 'email' | 'hostname' | 'ipv4' | 'ipv6' | 'uuid' | 'uri';
37
+ } & BasedSchemaFieldShared;
38
+ export type BasedSchemaFieldEnum = {
39
+ enum: any[];
40
+ } & BasedSchemaFieldShared;
41
+ export type BasedSchemaFieldConst = {
42
+ const: any;
43
+ } & BasedSchemaFieldShared;
44
+ type NumberDefaults = {
45
+ multipleOf?: number;
46
+ minimum?: number;
47
+ maximum?: number;
48
+ exclusiveMaximum?: boolean;
49
+ exclusiveMinimum?: boolean;
50
+ };
51
+ export type BasedSchemaFieldNumber = NumberDefaults & {
52
+ type: 'number';
53
+ };
54
+ export type BasedSchemaFieldHyperLogLog = {
55
+ type: 'hyperloglog';
56
+ };
57
+ export type BasedSchemaFieldInteger = NumberDefaults & {
58
+ type: 'integer';
59
+ } & BasedSchemaFieldShared;
60
+ export type BasedSchemaFieldTimeStamp = NumberDefaults & {
61
+ type: 'timestamp';
62
+ } & BasedSchemaFieldShared;
63
+ export type BasedSchemaFieldBoolean = {
64
+ type: 'boolean';
65
+ } & BasedSchemaFieldShared;
66
+ export type BasedSchemaFieldJSON = {
67
+ type: 'json';
68
+ } & BasedSchemaFieldShared;
69
+ export type BasedSchemaFieldPrimitive = BasedSchemaFieldString | BasedSchemaFieldNumber | BasedSchemaFieldInteger | BasedSchemaFieldTimeStamp | BasedSchemaFieldJSON | BasedSchemaFieldBoolean | BasedSchemaFieldEnum | BasedSchemaFieldShared;
70
+ export type BasedSchemaFieldText = {
71
+ type: 'text';
72
+ required?: BasedSchemaLanguage[];
73
+ contentMediaType?: BasedSchemaContentMediaType;
74
+ minLength?: number;
75
+ maxLength?: number;
76
+ contentMediaEncoding?: string;
77
+ pattern?: BasedSchemaPattern;
78
+ } & BasedSchemaFieldShared;
79
+ export type BasedSchemaFieldObject = {
80
+ type: 'object';
81
+ properties: {
82
+ [name: string]: BasedSchemaField;
83
+ };
84
+ required?: string[];
85
+ } & BasedSchemaFieldShared;
86
+ export type BasedSchemaFieldRecord = {
87
+ type: 'record';
88
+ values: BasedSchemaField;
89
+ } & BasedSchemaFieldShared;
90
+ export type BasedSchemaFieldArray = {
91
+ type: 'array';
92
+ values: BasedSchemaField;
93
+ } & BasedSchemaFieldShared;
94
+ export type BasedSchemaFieldSet = {
95
+ type: 'set';
96
+ items: BasedSchemaFieldPrimitive;
97
+ } & BasedSchemaFieldShared;
98
+ export type BasedSchemaFieldEnumerable = BasedSchemaFieldText | BasedSchemaFieldObject | BasedSchemaFieldRecord | BasedSchemaFieldArray | BasedSchemaFieldSet;
99
+ export type BasedSchemaFieldReference = {
100
+ type: 'reference';
101
+ bidirectional?: {
102
+ fromField: string;
103
+ };
104
+ allowedTypes?: AllowedTypes;
105
+ } & BasedSchemaFieldShared;
106
+ export type BasedSchemaFieldReferences = {
107
+ type: 'references';
108
+ bidirectional?: {
109
+ fromField: string;
110
+ };
111
+ allowedTypes?: AllowedTypes;
112
+ } & BasedSchemaFieldShared;
113
+ export type BasedSchemaField = BasedSchemaFieldEnumerable | BasedSchemaFieldPrimitive | BasedSchemaFieldReference | BasedSchemaFieldReferences | BasedSchemaFieldHyperLogLog | {
114
+ isRequired?: boolean;
115
+ $ref: string;
116
+ };
117
+ export type BasedSchemaType = {
118
+ fields: {
119
+ [name: string]: BasedSchemaField;
120
+ };
121
+ title?: string;
122
+ description?: string;
123
+ prefix?: BasedSchemaTypePrefix;
124
+ examples?: any[];
125
+ required?: string[];
126
+ $defs?: {
127
+ [key: string]: BasedSchemaField;
128
+ };
129
+ };
130
+ export type BasedSchema = {
131
+ languages: BasedSchemaLanguage[];
132
+ root: BasedSchemaType;
133
+ $defs: {
134
+ [name: string]: BasedSchemaField;
135
+ };
136
+ types: {
137
+ [type: string]: BasedSchemaType;
138
+ };
139
+ prefixToTypeMapping: {
140
+ [prefix: string]: string;
141
+ };
142
+ };
143
+ export type BasedSchemaTypePartial = PartialDeep<BasedSchemaType>;
144
+ export type BasedSchemaFieldPartial = PartialDeep<BasedSchemaField>;
145
+ export type BasedSchemaPartial = PartialDeep<BasedSchema>;
146
+ export type BasedSetTarget = {
147
+ type: string;
148
+ $alias?: string;
149
+ $id?: string;
150
+ schema: BasedSchema;
151
+ $language?: BasedSchemaLanguage;
152
+ };
153
+ export type BasedSetHandlers = {
154
+ collect: (props: {
155
+ path: (string | number)[];
156
+ value: any;
157
+ typeSchema: BasedSchemaType;
158
+ fieldSchema: BasedSchemaField;
159
+ target: BasedSetTarget;
160
+ }) => void;
161
+ referenceFilterCondition: (referenceId: string, $filter: any) => Promise<boolean>;
162
+ };
163
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isCollection = void 0;
4
+ const isCollection = (type) => {
5
+ return type === 'array' || type === 'object' || type === 'record';
6
+ };
7
+ exports.isCollection = isCollection;
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAqCO,MAAM,YAAY,GAAG,CAAC,IAAY,EAAW,EAAE;IACpD,OAAO,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,QAAQ,CAAA;AACnE,CAAC,CAAA;AAFY,QAAA,YAAY,gBAExB"}
@@ -0,0 +1,4 @@
1
+ import { BasedSchemaPartial, BasedSchemaFieldPartial, BasedSchemaTypePartial } from './types';
2
+ export declare const parseType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
3
+ export declare const parseField: (fromSchema: BasedSchemaPartial, path: string[], field: BasedSchemaFieldPartial) => void;
4
+ export declare const validateSchema: (schema: BasedSchemaPartial) => BasedSchemaPartial;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchema = exports.parseField = exports.parseType = void 0;
4
+ const parseType = (fromSchema, typeName, type) => {
5
+ if (type.prefix &&
6
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
7
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
8
+ }
9
+ };
10
+ exports.parseType = parseType;
11
+ const parseField = (fromSchema, path, field) => { };
12
+ exports.parseField = parseField;
13
+ const validateSchema = (schema) => {
14
+ // rewrite schema things like required / required: []
15
+ if (typeof schema !== 'object') {
16
+ throw new Error('Schema is not an object');
17
+ }
18
+ if (schema.languages && !Array.isArray(schema.languages)) {
19
+ throw new Error('Languages needs to be an array');
20
+ }
21
+ if (schema.$defs) {
22
+ // first defs ofc
23
+ }
24
+ if (schema.root) {
25
+ }
26
+ if (schema.types) {
27
+ for (const type in schema.types) {
28
+ (0, exports.parseType)(schema, type, schema.types[type]);
29
+ }
30
+ }
31
+ return schema;
32
+ };
33
+ exports.validateSchema = validateSchema;
34
+ //# sourceMappingURL=validate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":";;;AAOO,MAAM,SAAS,GAAG,CACvB,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,SAAS,aAarB;AAEM,MAAM,UAAU,GAAG,CACxB,UAA8B,EAC9B,IAAc,EACd,KAA8B,EAC9B,EAAE,GAAE,CAAC,CAAA;AAJM,QAAA,UAAU,cAIhB;AAEA,MAAM,cAAc,GAAG,CAC5B,MAA0B,EACN,EAAE;IACtB,qDAAqD;IAErD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;KAClD;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,iBAAiB;KAClB;IAED,IAAI,MAAM,CAAC,IAAI,EAAE;KAChB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;YAC/B,IAAA,iBAAS,EAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;SAC5C;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA3BY,QAAA,cAAc,kBA2B1B"}
@@ -0,0 +1,4 @@
1
+ import { BasedSchemaPartial, BasedSchemaFieldPartial, BasedSchemaTypePartial } from './types';
2
+ export declare const parseType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
3
+ export declare const parseField: (fromSchema: BasedSchemaPartial, path: string[], field: BasedSchemaFieldPartial) => void;
4
+ export declare const validateSchema: (schema: BasedSchemaPartial) => BasedSchemaPartial;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchema = exports.parseField = exports.parseType = void 0;
4
+ const parseType = (fromSchema, typeName, type) => {
5
+ if (type.prefix &&
6
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
7
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
8
+ }
9
+ };
10
+ exports.parseType = parseType;
11
+ const parseField = (fromSchema, path, field) => { };
12
+ exports.parseField = parseField;
13
+ const validateSchema = (schema) => {
14
+ // rewrite schema things like required / required: []
15
+ if (typeof schema !== 'object') {
16
+ throw new Error('Schema is not an object');
17
+ }
18
+ if (schema.languages && !Array.isArray(schema.languages)) {
19
+ throw new Error('Languages needs to be an array');
20
+ }
21
+ if (schema.$defs) {
22
+ // first defs ofc
23
+ }
24
+ if (schema.root) {
25
+ }
26
+ if (schema.types) {
27
+ for (const type in schema.types) {
28
+ (0, exports.parseType)(schema, type, schema.types[type]);
29
+ }
30
+ }
31
+ return schema;
32
+ };
33
+ exports.validateSchema = validateSchema;
34
+ //# sourceMappingURL=validateFields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateFields.js","sourceRoot":"","sources":["../src/validateFields.ts"],"names":[],"mappings":";;;AAOO,MAAM,SAAS,GAAG,CACvB,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,SAAS,aAarB;AAEM,MAAM,UAAU,GAAG,CACxB,UAA8B,EAC9B,IAAc,EACd,KAA8B,EAC9B,EAAE,GAAE,CAAC,CAAA;AAJM,QAAA,UAAU,cAIhB;AAEA,MAAM,cAAc,GAAG,CAC5B,MAA0B,EACN,EAAE;IACtB,qDAAqD;IAErD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;KAClD;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,iBAAiB;KAClB;IAED,IAAI,MAAM,CAAC,IAAI,EAAE;KAChB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;YAC/B,IAAA,iBAAS,EAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;SAC5C;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA3BY,QAAA,cAAc,kBA2B1B"}
@@ -0,0 +1,4 @@
1
+ import { BasedSchemaPartial, BasedSchemaFieldPartial, BasedSchemaTypePartial } from './types';
2
+ export declare const parseType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
3
+ export declare const parseField: (fromSchema: BasedSchemaPartial, path: string[], field: BasedSchemaFieldPartial) => void;
4
+ export declare const validateSchema: (schema: BasedSchemaPartial) => BasedSchemaPartial;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchema = exports.parseField = exports.parseType = void 0;
4
+ const parseType = (fromSchema, typeName, type) => {
5
+ if (type.prefix &&
6
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
7
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
8
+ }
9
+ };
10
+ exports.parseType = parseType;
11
+ const parseField = (fromSchema, path, field) => { };
12
+ exports.parseField = parseField;
13
+ const validateSchema = (schema) => {
14
+ // rewrite schema things like required / required: []
15
+ if (typeof schema !== 'object') {
16
+ throw new Error('Schema is not an object');
17
+ }
18
+ if (schema.languages && !Array.isArray(schema.languages)) {
19
+ throw new Error('Languages needs to be an array');
20
+ }
21
+ if (schema.$defs) {
22
+ // first defs ofc
23
+ }
24
+ if (schema.root) {
25
+ }
26
+ if (schema.types) {
27
+ for (const type in schema.types) {
28
+ (0, exports.parseType)(schema, type, schema.types[type]);
29
+ }
30
+ }
31
+ return schema;
32
+ };
33
+ exports.validateSchema = validateSchema;
34
+ //# sourceMappingURL=validateSchema%20copy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateSchema copy.js","sourceRoot":"","sources":["../src/validateSchema copy.ts"],"names":[],"mappings":";;;AAOO,MAAM,SAAS,GAAG,CACvB,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,SAAS,aAarB;AAEM,MAAM,UAAU,GAAG,CACxB,UAA8B,EAC9B,IAAc,EACd,KAA8B,EAC9B,EAAE,GAAE,CAAC,CAAA;AAJM,QAAA,UAAU,cAIhB;AAEA,MAAM,cAAc,GAAG,CAC5B,MAA0B,EACN,EAAE;IACtB,qDAAqD;IAErD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;KAClD;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,iBAAiB;KAClB;IAED,IAAI,MAAM,CAAC,IAAI,EAAE;KAChB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;YAC/B,IAAA,iBAAS,EAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;SAC5C;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA3BY,QAAA,cAAc,kBA2B1B"}
@@ -0,0 +1,4 @@
1
+ import { BasedSchemaPartial, BasedSchemaFieldPartial, BasedSchemaTypePartial } from './types';
2
+ export declare const validateType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
3
+ export declare const validateField: (fromSchema: BasedSchemaPartial, path: string[], field: BasedSchemaFieldPartial) => void;
4
+ export declare const validateSchema: (schema: BasedSchemaPartial) => BasedSchemaPartial;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchema = exports.validateField = exports.validateType = void 0;
4
+ const validateType = (fromSchema, typeName, type) => {
5
+ if (type.prefix &&
6
+ (typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
7
+ throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
8
+ }
9
+ };
10
+ exports.validateType = validateType;
11
+ const validateField = (fromSchema, path, field) => { };
12
+ exports.validateField = validateField;
13
+ const validateSchema = (schema) => {
14
+ // rewrite schema things like required / required: []
15
+ if (typeof schema !== 'object') {
16
+ throw new Error('Schema is not an object');
17
+ }
18
+ if (schema.languages && !Array.isArray(schema.languages)) {
19
+ throw new Error('Languages needs to be an array');
20
+ }
21
+ if (schema.$defs) {
22
+ // first defs ofc
23
+ }
24
+ if (schema.root) {
25
+ }
26
+ if (schema.types) {
27
+ for (const type in schema.types) {
28
+ (0, exports.validateType)(schema, type, schema.types[type]);
29
+ }
30
+ }
31
+ return schema;
32
+ };
33
+ exports.validateSchema = validateSchema;
34
+ //# sourceMappingURL=validateSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateSchema.js","sourceRoot":"","sources":["../src/validateSchema.ts"],"names":[],"mappings":";;;AAOO,MAAM,YAAY,GAAG,CAC1B,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,YAAY,gBAaxB;AAEM,MAAM,aAAa,GAAG,CAC3B,UAA8B,EAC9B,IAAc,EACd,KAA8B,EAC9B,EAAE,GAAE,CAAC,CAAA;AAJM,QAAA,aAAa,iBAInB;AAEA,MAAM,cAAc,GAAG,CAC5B,MAA0B,EACN,EAAE;IACtB,qDAAqD;IAErD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE;QACxD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;KAClD;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,iBAAiB;KAClB;IAED,IAAI,MAAM,CAAC,IAAI,EAAE;KAChB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;YAC/B,IAAA,oBAAY,EAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;SAC/C;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA3BY,QAAA,cAAc,kBA2B1B"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@based/schema",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "npx tsc",
8
+ "watch": "npx tsc --watch",
9
+ "clean": "rimraf {.turbo,dist,node_modules}",
10
+ "test": "ava --color --node-arguments='--expose-gc' --timeout 3m --verbose"
11
+ },
12
+ "sideEffects": false,
13
+ "dependencies": {
14
+ "@saulx/utils": "^3.2.2"
15
+ },
16
+ "ava": {
17
+ "files": [
18
+ "test/*.ts"
19
+ ],
20
+ "concurrency": 1,
21
+ "extensions": [
22
+ "ts"
23
+ ],
24
+ "require": [
25
+ "ts-node/register"
26
+ ]
27
+ },
28
+ "devDependencies": {
29
+ "type-fest": "^3.12.0",
30
+ "ts-node": "10.9.1",
31
+ "typescript": "^4.3.5",
32
+ "rimraf": "^3.0.2",
33
+ "ava": "3.15.0"
34
+ }
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './types'
2
+
3
+ export * from './validateSchema'
4
+
5
+ export * from './set'
@@ -0,0 +1,188 @@
1
+ export const languages = {
2
+ ab: 'Abkhazian',
3
+ aa: 'Afar',
4
+ af: 'Afrikaans',
5
+ ak: 'Akan',
6
+ sq: 'Albanian',
7
+ am: 'Amharic',
8
+ ar: 'Arabic',
9
+ an: 'Aragonese',
10
+ hy: 'Armenian',
11
+ as: 'Assamese',
12
+ av: 'Avaric',
13
+ ae: 'Avestan',
14
+ ay: 'Aymara',
15
+ az: 'Azerbaijani',
16
+ bm: 'Bambara',
17
+ ba: 'Bashkir',
18
+ eu: 'Basque',
19
+ be: 'Belarusian',
20
+ bn: 'Bengali',
21
+ bh: 'Bihari languages',
22
+ bi: 'Bislama',
23
+ bs: 'Bosnian',
24
+ br: 'Breton',
25
+ bg: 'Bulgarian',
26
+ my: 'Burmese',
27
+ ca: 'Catalan, Valencian',
28
+ km: 'Central Khmer',
29
+ ch: 'Chamorro',
30
+ ce: 'Chechen',
31
+ ny: 'Chichewa, Chewa, Nyanja',
32
+ zh: 'Chinese',
33
+ cu: 'Church Slavonic, Old Bulgarian, Old Church Slavonic',
34
+ cv: 'Chuvash',
35
+ kw: 'Cornish',
36
+ co: 'Corsican',
37
+ cr: 'Cree',
38
+ hr: 'Croatian',
39
+ cs: 'Czech',
40
+ da: 'Danish',
41
+ dv: 'Divehi, Dhivehi, Maldivian',
42
+ nl: 'Dutch, Flemish',
43
+ dz: 'Dzongkha',
44
+ en: 'English',
45
+ eo: 'Esperanto',
46
+ et: 'Estonian',
47
+ ee: 'Ewe',
48
+ fo: 'Faroese',
49
+ fj: 'Fijian',
50
+ fi: 'Finnish',
51
+ fr: 'French',
52
+ ff: 'Fulah',
53
+ gd: 'Gaelic, Scottish Gaelic',
54
+ gl: 'Galician',
55
+ lg: 'Ganda',
56
+ ka: 'Georgian',
57
+ de: 'German',
58
+ ki: 'Gikuyu, Kikuyu',
59
+ el: 'Greek (Modern)',
60
+ kl: 'Greenlandic, Kalaallisut',
61
+ gn: 'Guarani',
62
+ gu: 'Gujarati',
63
+ ht: 'Haitian, Haitian Creole',
64
+ ha: 'Hausa',
65
+ he: 'Hebrew',
66
+ hz: 'Herero',
67
+ hi: 'Hindi',
68
+ ho: 'Hiri Motu',
69
+ hu: 'Hungarian',
70
+ is: 'Icelandic',
71
+ io: 'Ido',
72
+ ig: 'Igbo',
73
+ id: 'Indonesian',
74
+ ia: 'Interlingua (International Auxiliary Language Association)',
75
+ ie: 'Interlingue',
76
+ iu: 'Inuktitut',
77
+ ik: 'Inupiaq',
78
+ ga: 'Irish',
79
+ it: 'Italian',
80
+ ja: 'Japanese',
81
+ jv: 'Javanese',
82
+ kn: 'Kannada',
83
+ kr: 'Kanuri',
84
+ ks: 'Kashmiri',
85
+ kk: 'Kazakh',
86
+ rw: 'Kinyarwanda',
87
+ kv: 'Komi',
88
+ kg: 'Kongo',
89
+ ko: 'Korean',
90
+ kj: 'Kwanyama, Kuanyama',
91
+ ku: 'Kurdish',
92
+ ky: 'Kyrgyz',
93
+ lo: 'Lao',
94
+ la: 'Latin',
95
+ lv: 'Latvian',
96
+ lb: 'Letzeburgesch, Luxembourgish',
97
+ li: 'Limburgish, Limburgan, Limburger',
98
+ ln: 'Lingala',
99
+ lt: 'Lithuanian',
100
+ lu: 'Luba-Katanga',
101
+ mk: 'Macedonian',
102
+ mg: 'Malagasy',
103
+ ms: 'Malay',
104
+ ml: 'Malayalam',
105
+ mt: 'Maltese',
106
+ gv: 'Manx',
107
+ mi: 'Maori',
108
+ mr: 'Marathi',
109
+ mh: 'Marshallese',
110
+ ro: 'Moldovan, Moldavian, Romanian',
111
+ mn: 'Mongolian',
112
+ na: 'Nauru',
113
+ nv: 'Navajo, Navaho',
114
+ nd: 'Northern Ndebele',
115
+ ng: 'Ndonga',
116
+ ne: 'Nepali',
117
+ se: 'Northern Sami',
118
+ no: 'Norwegian',
119
+ nb: 'Norwegian Bokmål',
120
+ nn: 'Norwegian Nynorsk',
121
+ ii: 'Nuosu, Sichuan Yi',
122
+ oc: 'Occitan (post 1500)',
123
+ oj: 'Ojibwa',
124
+ or: 'Oriya',
125
+ om: 'Oromo',
126
+ os: 'Ossetian, Ossetic',
127
+ pi: 'Pali',
128
+ pa: 'Panjabi, Punjabi',
129
+ ps: 'Pashto, Pushto',
130
+ fa: 'Persian',
131
+ pl: 'Polish',
132
+ pt: 'Portuguese',
133
+ qu: 'Quechua',
134
+ rm: 'Romansh',
135
+ rn: 'Rundi',
136
+ ru: 'Russian',
137
+ sm: 'Samoan',
138
+ sg: 'Sango',
139
+ sa: 'Sanskrit',
140
+ sc: 'Sardinian',
141
+ sr: 'Serbian',
142
+ sn: 'Shona',
143
+ sd: 'Sindhi',
144
+ si: 'Sinhala, Sinhalese',
145
+ sk: 'Slovak',
146
+ sl: 'Slovenian',
147
+ so: 'Somali',
148
+ st: 'Sotho, Southern',
149
+ nr: 'South Ndebele',
150
+ es: 'Spanish, Castilian',
151
+ su: 'Sundanese',
152
+ sw: 'Swahili',
153
+ ss: 'Swati',
154
+ sv: 'Swedish',
155
+ tl: 'Tagalog',
156
+ ty: 'Tahitian',
157
+ tg: 'Tajik',
158
+ ta: 'Tamil',
159
+ tt: 'Tatar',
160
+ te: 'Telugu',
161
+ th: 'Thai',
162
+ bo: 'Tibetan',
163
+ ti: 'Tigrinya',
164
+ to: 'Tonga (Tonga Islands)',
165
+ ts: 'Tsonga',
166
+ tn: 'Tswana',
167
+ tr: 'Turkish',
168
+ tk: 'Turkmen',
169
+ tw: 'Twi',
170
+ ug: 'Uighur, Uyghur',
171
+ uk: 'Ukrainian',
172
+ ur: 'Urdu',
173
+ uz: 'Uzbek',
174
+ ve: 'Venda',
175
+ vi: 'Vietnamese',
176
+ vo: 'Volap_k',
177
+ wa: 'Walloon',
178
+ cy: 'Welsh',
179
+ fy: 'Western Frisian',
180
+ wo: 'Wolof',
181
+ xh: 'Xhosa',
182
+ yi: 'Yiddish',
183
+ yo: 'Yoruba',
184
+ za: 'Zhuang, Chuang',
185
+ zu: 'Zulu',
186
+ }
187
+
188
+ export type Language = keyof typeof languages
@@ -0,0 +1,15 @@
1
+ export const createError = (
2
+ path: (number | string)[],
3
+ fromType: string,
4
+ fieldType: string,
5
+ value: any,
6
+ fieldDoesNotExist?: string,
7
+ msg?: string
8
+ ): Error => {
9
+ const err = new Error()
10
+ return new Error(
11
+ `Type: "${fromType}" Field: "${path.join(
12
+ '.'
13
+ )}" is not a valid value for ${fieldType}`
14
+ )
15
+ }