@based/schema 4.1.1 → 5.0.0-alpha.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 (123) hide show
  1. package/README.md +152 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +2 -0
  4. package/dist/lang.d.ts +153 -0
  5. package/dist/lang.js +153 -0
  6. package/dist/mermaid.d.ts +3 -0
  7. package/dist/mermaid.js +24 -0
  8. package/dist/parse/assert.d.ts +2 -1
  9. package/dist/parse/assert.js +9 -3
  10. package/dist/parse/errors.d.ts +2 -0
  11. package/dist/parse/errors.js +2 -0
  12. package/dist/parse/index.d.ts +5 -4
  13. package/dist/parse/index.js +43 -41
  14. package/dist/parse/props.js +168 -75
  15. package/dist/parse/utils.d.ts +2 -2
  16. package/dist/parse/utils.js +11 -2
  17. package/dist/types.d.ts +72 -23
  18. package/dist/types.js +93 -0
  19. package/package.json +4 -3
  20. package/dist/parsePayload/index.d.ts +0 -3
  21. package/dist/parsePayload/index.js +0 -2
  22. package/dist/parseSchema/assert.d.ts +0 -6
  23. package/dist/parseSchema/assert.js +0 -27
  24. package/dist/parseSchema/errors.d.ts +0 -19
  25. package/dist/parseSchema/errors.js +0 -19
  26. package/dist/parseSchema/index.d.ts +0 -20
  27. package/dist/parseSchema/index.js +0 -132
  28. package/dist/parseSchema/props.d.ts +0 -7
  29. package/dist/parseSchema/props.js +0 -256
  30. package/dist/parseSchema/utils.d.ts +0 -3
  31. package/dist/parseSchema/utils.js +0 -29
  32. package/dist/src/compat/index.d.ts +0 -6
  33. package/dist/src/compat/index.js +0 -217
  34. package/dist/src/compat/oldSchemaType.d.ts +0 -93
  35. package/dist/src/compat/oldSchemaType.js +0 -2
  36. package/dist/src/display/index.d.ts +0 -2
  37. package/dist/src/display/index.js +0 -16
  38. package/dist/src/display/number.d.ts +0 -4
  39. package/dist/src/display/number.js +0 -97
  40. package/dist/src/display/string.d.ts +0 -5
  41. package/dist/src/display/string.js +0 -26
  42. package/dist/src/display/timestamp.d.ts +0 -5
  43. package/dist/src/display/timestamp.js +0 -134
  44. package/dist/src/error.d.ts +0 -21
  45. package/dist/src/error.js +0 -23
  46. package/dist/src/index.d.ts +0 -6
  47. package/dist/src/index.js +0 -7
  48. package/dist/src/languages.d.ts +0 -188
  49. package/dist/src/languages.js +0 -188
  50. package/dist/src/set/fields/array.d.ts +0 -2
  51. package/dist/src/set/fields/array.js +0 -119
  52. package/dist/src/set/fields/index.d.ts +0 -3
  53. package/dist/src/set/fields/index.js +0 -85
  54. package/dist/src/set/fields/number.d.ts +0 -4
  55. package/dist/src/set/fields/number.js +0 -123
  56. package/dist/src/set/fields/object.d.ts +0 -3
  57. package/dist/src/set/fields/object.js +0 -28
  58. package/dist/src/set/fields/references.d.ts +0 -3
  59. package/dist/src/set/fields/references.js +0 -136
  60. package/dist/src/set/fields/set.d.ts +0 -2
  61. package/dist/src/set/fields/set.js +0 -59
  62. package/dist/src/set/fields/string.d.ts +0 -3
  63. package/dist/src/set/fields/string.js +0 -289
  64. package/dist/src/set/index.d.ts +0 -3
  65. package/dist/src/set/index.js +0 -205
  66. package/dist/src/set/isValidId.d.ts +0 -2
  67. package/dist/src/set/isValidId.js +0 -17
  68. package/dist/src/types.d.ts +0 -205
  69. package/dist/src/types.js +0 -113
  70. package/dist/src/updateSchema.d.ts +0 -2
  71. package/dist/src/updateSchema.js +0 -12
  72. package/dist/src/validateSchema/basedSchemaTypeValidator.d.ts +0 -3
  73. package/dist/src/validateSchema/basedSchemaTypeValidator.js +0 -45
  74. package/dist/src/validateSchema/fieldValidators.d.ts +0 -27
  75. package/dist/src/validateSchema/fieldValidators.js +0 -360
  76. package/dist/src/validateSchema/index.d.ts +0 -17
  77. package/dist/src/validateSchema/index.js +0 -109
  78. package/dist/src/validateSchema/utils.d.ts +0 -25
  79. package/dist/src/validateSchema/utils.js +0 -61
  80. package/dist/src/walker/args.d.ts +0 -36
  81. package/dist/src/walker/args.js +0 -158
  82. package/dist/src/walker/index.d.ts +0 -6
  83. package/dist/src/walker/index.js +0 -31
  84. package/dist/src/walker/parse.d.ts +0 -3
  85. package/dist/src/walker/parse.js +0 -182
  86. package/dist/src/walker/types.d.ts +0 -45
  87. package/dist/src/walker/types.js +0 -7
  88. package/dist/test/array.d.ts +0 -1
  89. package/dist/test/array.js +0 -342
  90. package/dist/test/compat.d.ts +0 -1
  91. package/dist/test/compat.js +0 -97
  92. package/dist/test/data/newSchemas.d.ts +0 -2
  93. package/dist/test/data/newSchemas.js +0 -489
  94. package/dist/test/data/oldSchemas.d.ts +0 -2
  95. package/dist/test/data/oldSchemas.js +0 -5058
  96. package/dist/test/display.d.ts +0 -1
  97. package/dist/test/display.js +0 -103
  98. package/dist/test/json.d.ts +0 -1
  99. package/dist/test/json.js +0 -40
  100. package/dist/test/number.d.ts +0 -1
  101. package/dist/test/number.js +0 -374
  102. package/dist/test/reference.d.ts +0 -1
  103. package/dist/test/reference.js +0 -188
  104. package/dist/test/rest.d.ts +0 -1
  105. package/dist/test/rest.js +0 -168
  106. package/dist/test/set.d.ts +0 -1
  107. package/dist/test/set.js +0 -95
  108. package/dist/test/string.d.ts +0 -1
  109. package/dist/test/string.js +0 -120
  110. package/dist/test/text.d.ts +0 -1
  111. package/dist/test/text.js +0 -324
  112. package/dist/test/utils/index.d.ts +0 -11
  113. package/dist/test/utils/index.js +0 -17
  114. package/dist/test/validateSchema/basic.d.ts +0 -1
  115. package/dist/test/validateSchema/basic.js +0 -94
  116. package/dist/test/validateSchema/fields.d.ts +0 -1
  117. package/dist/test/validateSchema/fields.js +0 -436
  118. package/dist/test/validateSchema/languages.d.ts +0 -1
  119. package/dist/test/validateSchema/languages.js +0 -124
  120. package/dist/test/validateSchema/realWorld.d.ts +0 -1
  121. package/dist/test/validateSchema/realWorld.js +0 -13
  122. package/dist/test/walker.d.ts +0 -1
  123. package/dist/test/walker.js +0 -289
package/dist/types.d.ts CHANGED
@@ -1,4 +1,15 @@
1
+ import { LangName } from './lang.js';
2
+ type Role = 'title' | 'source' | 'media' | string;
3
+ export declare const numberDisplays: readonly ["short", "human", "ratio", "bytes", "euro", "dollar", "pound", "meter"];
4
+ export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "time", "time-precise"];
5
+ export declare const stringFormats: readonly ["alpha", "alphaLocales", "alphanumeric", "alphanumericLocales", "ascii", "base32", "base58", "base64", "BIC", "btcAddress", "clike", "code", "creditCard", "css", "currency", "dataURI", "EAN", "email", "ethereumAddress", "FQDN", "hexadecimal", "hexColor", "HSL", "html", "IBAN", "identityCard", "IMEI", "IP", "IPRange", "ISBN", "ISIN", "ISO31661Alpha2", "ISO31661Alpha3", "ISO4217", "ISO6391", "ISO8601", "ISRC", "ISSN", "javascript", "json", "JWT", "latLong", "licensePlate", "lowercase", "luhnNumber", "MACAddress", "magnetURI", "markdown", "MD5", "mimeType", "mobilePhone", "mobilePhoneLocales", "octal", "passportNumber", "port", "postalCode", "postalCodeLocales", "python", "RFC3339", "rgbColor", "rust", "semVer", "slug", "strongPassword", "surrogatePair", "taxID", "typescript", "uppercase", "URL", "UUID", "VAT", "multiline"];
6
+ type DateDisplay = (typeof dateDisplays)[number];
7
+ type NumberDisplay = (typeof numberDisplays)[number] | `round-${number}`;
8
+ type StringFormat = (typeof stringFormats)[number];
9
+ type MimeString = 'text/html' | 'text/plain' | 'text/markdown' | 'image/png' | 'image/jpeg' | 'video/mp4' | 'video/quicktime' | 'image/*' | 'video/*' | 'audio/*' | '*/*' | `${string}/${string}`;
10
+ type Mime = MimeString | MimeString[];
1
11
  type Letter = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
12
+ type AllowedKey = `${Letter}${string}`;
2
13
  type QueryFn = Function;
3
14
  type PropValues = {
4
15
  type?: string;
@@ -6,19 +17,25 @@ type PropValues = {
6
17
  };
7
18
  type Prop<V extends PropValues> = {
8
19
  required?: boolean;
9
- label?: Record<string, string>;
10
- description?: Record<string, string>;
20
+ title?: string | Record<string, string>;
21
+ description?: string | Record<string, string>;
11
22
  path?: string;
12
23
  query?: QueryFn;
24
+ role?: Role;
25
+ readOnly?: boolean;
26
+ examples?: string[];
13
27
  } & V;
14
28
  type EnumItem = string | number | boolean;
29
+ type NeverInItems = {
30
+ required?: never;
31
+ };
15
32
  export type SchemaReferences = Prop<{
16
33
  type?: 'references';
17
- items: SchemaReference;
34
+ items: SchemaReference & NeverInItems;
18
35
  }>;
19
36
  export type SchemaReferencesOneWay = Prop<{
20
37
  type?: 'references';
21
- items: SchemaReferenceOneWay;
38
+ items: SchemaReferenceOneWay & NeverInItems;
22
39
  }>;
23
40
  export type SchemaText = Prop<{
24
41
  type: 'text';
@@ -31,6 +48,7 @@ export type SchemaNumber = Prop<{
31
48
  min?: number;
32
49
  max?: number;
33
50
  step?: number | 'any';
51
+ display?: NumberDisplay;
34
52
  }>;
35
53
  export type SchemaString = Prop<{
36
54
  type: 'string';
@@ -38,31 +56,49 @@ export type SchemaString = Prop<{
38
56
  maxBytes?: number;
39
57
  max?: number;
40
58
  min?: number;
59
+ mime?: Mime;
60
+ format?: StringFormat;
61
+ compression?: 'none' | 'deflate';
41
62
  }>;
42
63
  export type SchemaBinary = Prop<{
43
64
  type: 'binary';
44
65
  default?: ArrayBuffer;
45
66
  maxBytes?: number;
67
+ mime?: Mime;
68
+ format?: StringFormat;
46
69
  }>;
47
70
  export type SchemaBoolean = Prop<{
48
71
  type: 'boolean';
49
72
  default?: boolean;
50
73
  }>;
74
+ export type SchemaHll = Prop<{
75
+ type: 'hll';
76
+ default?: number;
77
+ }>;
78
+ export type SchemaVector = Prop<{
79
+ type: 'vector';
80
+ default?: Float32Array;
81
+ size: number;
82
+ }>;
51
83
  export type SchemaTimestamp = Prop<{
52
84
  type: 'timestamp';
53
85
  default?: number | Date;
54
86
  on?: 'create' | 'update';
87
+ display?: DateDisplay;
55
88
  }>;
56
89
  export type SchemaReferenceOneWay = Prop<{
57
90
  type?: 'reference';
58
91
  default?: string;
59
92
  ref: string;
93
+ mime?: Mime;
60
94
  }>;
61
95
  export type SchemaReference = Prop<{
62
96
  type?: 'reference';
63
97
  default?: string;
64
98
  ref: string;
65
99
  prop: string;
100
+ dependent?: boolean;
101
+ mime?: Mime;
66
102
  }> & Record<`$${string}`, SchemaPropOneWay>;
67
103
  export type SchemaObject = Prop<{
68
104
  type?: 'object';
@@ -83,43 +119,54 @@ export type SchemaEnum = Prop<{
83
119
  default?: EnumItem;
84
120
  enum: EnumItem[];
85
121
  }>;
86
- export type SchemaAlias = SchemaString & {
122
+ export type SchemaAlias = Omit<SchemaString, 'type'> & {
87
123
  type: 'alias';
88
124
  };
89
- export type SchemaPropShorthand = 'timestamp' | 'boolean' | 'string' | 'alias' | 'text' | NumberType | EnumItem[];
90
- type SetItems = SchemaPropShorthand | SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaEnum;
125
+ export type SchemaPropShorthand = 'timestamp' | 'binary' | 'boolean' | 'string' | 'alias' | 'text' | 'hll' | NumberType | EnumItem[];
126
+ type SetItems<isStrict = false> = SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaEnum | (isStrict extends true ? never : 'timestamp' | 'binary' | 'boolean' | 'string' | NumberType | EnumItem[]);
91
127
  export type SchemaSet<ItemsType extends SetItems = SetItems> = Prop<{
92
128
  type?: 'set';
93
129
  default?: ItemsType extends {
94
130
  default: any;
95
131
  } ? ItemsType['default'][] : undefined;
96
- items: ItemsType;
132
+ items: ItemsType & NeverInItems;
97
133
  }>;
98
- type NonRefSchemaProps = SchemaPropShorthand | SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaAlias | SchemaText | SchemaEnum | SchemaSet;
99
- export type SchemaProp = SchemaReferencesWithQuery | SchemaReferenceWithQuery | NonRefSchemaProps | SchemaReferences | SchemaReference | SchemaObject | SchemaBinary;
100
- export type SchemaPropOneWay = SchemaReferencesOneWay | SchemaReferenceOneWay | SchemaObjectOneWay | NonRefSchemaProps;
134
+ type NonRefSchemaProps<isStrict = false> = SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaAlias | SchemaText | SchemaEnum | SchemaBinary | SchemaHll | SchemaVector | (isStrict extends true ? SchemaSet<SetItems<true>> : SchemaPropShorthand | SchemaSet);
135
+ export type SchemaProp<isStrict = false> = SchemaReferencesWithQuery | SchemaReferenceWithQuery | NonRefSchemaProps<isStrict> | SchemaReferences | SchemaReference | SchemaObject | SchemaBinary;
136
+ export type SchemaPropOneWay<isStrict = false> = SchemaReferencesOneWay | SchemaReferenceOneWay | SchemaObjectOneWay | NonRefSchemaProps<isStrict>;
101
137
  export type SchemaAnyProp = SchemaPropOneWay | SchemaProp;
102
138
  export type SchemaHook = string | Function;
103
- export type SchemaProps = Record<string, SchemaProp>;
104
- export type SchemaType = {
139
+ export type SchemaProps<isStrict = false> = Record<AllowedKey, SchemaProp<isStrict>> & {
140
+ id?: never;
141
+ };
142
+ type GenericSchemaType<isStrict = false> = {
105
143
  hooks?: {
106
144
  create?: SchemaHook;
107
145
  update?: SchemaHook;
108
146
  delete?: SchemaHook;
109
147
  };
110
148
  id?: number;
111
- props: SchemaProps;
149
+ props: SchemaProps<isStrict>;
150
+ };
151
+ export type StrictSchemaType = GenericSchemaType<true>;
152
+ export type SchemaType<isStrict = false> = isStrict extends true ? StrictSchemaType : StrictSchemaType | GenericSchemaType<false> | (SchemaProps & {
153
+ props?: never;
154
+ });
155
+ export type SchemaTypeName = AllowedKey;
156
+ export type SchemaTypes<isStrict = false> = Record<SchemaTypeName, SchemaType<isStrict>>;
157
+ export type SchemaPropsOneWay<isStrict = false> = Record<AllowedKey, SchemaPropOneWay<isStrict>> & {
158
+ id?: never;
112
159
  };
113
- export type SchemaTypes = Record<string, SchemaType>;
114
- export type SchemaPropsOneWay = Record<`${Letter}${string}`, SchemaPropOneWay>;
115
- export type Schema = {
116
- types?: SchemaTypes;
117
- props?: SchemaPropsOneWay;
118
- locales?: SchemaLocales;
160
+ type GenericSchema<isStrict = false> = {
161
+ types?: SchemaTypes<isStrict>;
162
+ props?: SchemaPropsOneWay<isStrict>;
163
+ locales?: Partial<SchemaLocales>;
119
164
  };
120
- export type SchemaLocales = Record<string, {
165
+ export type StrictSchema = GenericSchema<true>;
166
+ export type Schema = GenericSchema<false> | StrictSchema;
167
+ export type SchemaLocales = Record<LangName, {
121
168
  required?: boolean;
122
- fallback?: string[];
169
+ fallback?: LangName[];
123
170
  }>;
124
171
  export type SchemaPropTypeMap = {
125
172
  references: SchemaReferences;
@@ -133,8 +180,10 @@ export type SchemaPropTypeMap = {
133
180
  text: SchemaText;
134
181
  set: SchemaSet;
135
182
  binary: SchemaBinary;
183
+ hll: SchemaHll;
184
+ vector: SchemaVector;
136
185
  } & Record<NumberType, SchemaNumber>;
137
186
  export type SchemaPropTypes = keyof SchemaPropTypeMap;
138
- export declare const isPropType: <T extends "string" | "boolean" | "object" | "references" | "timestamp" | "alias" | "text" | NumberType | "binary" | "reference" | "enum" | "set">(type: T, prop: SchemaProp) => prop is SchemaPropTypeMap[T];
187
+ export declare const isPropType: <T extends SchemaPropTypes>(type: T, prop: SchemaProp) => prop is SchemaPropTypeMap[T];
139
188
  export {};
140
189
  //# sourceMappingURL=types.d.ts.map
package/dist/types.js CHANGED
@@ -1,4 +1,97 @@
1
1
  import { getPropType } from './parse/utils.js';
2
+ export const numberDisplays = [
3
+ 'short',
4
+ 'human',
5
+ 'ratio',
6
+ 'bytes',
7
+ 'euro',
8
+ 'dollar',
9
+ 'pound',
10
+ 'meter',
11
+ ];
12
+ export const dateDisplays = [
13
+ 'date',
14
+ 'date-time',
15
+ 'date-time-text',
16
+ 'date-time-human',
17
+ 'time',
18
+ 'time-precise',
19
+ ];
20
+ export const stringFormats = [
21
+ 'alpha',
22
+ 'alphaLocales',
23
+ 'alphanumeric',
24
+ 'alphanumericLocales',
25
+ 'ascii',
26
+ 'base32',
27
+ 'base58',
28
+ 'base64',
29
+ 'BIC',
30
+ 'btcAddress',
31
+ 'clike',
32
+ 'code',
33
+ 'creditCard',
34
+ 'css',
35
+ 'currency',
36
+ 'dataURI',
37
+ 'EAN',
38
+ 'email',
39
+ 'ethereumAddress',
40
+ 'FQDN',
41
+ 'hexadecimal',
42
+ 'hexColor',
43
+ 'HSL',
44
+ 'html',
45
+ 'IBAN',
46
+ 'identityCard',
47
+ 'IMEI',
48
+ 'IP',
49
+ 'IPRange',
50
+ 'ISBN',
51
+ 'ISIN',
52
+ 'ISO31661Alpha2',
53
+ 'ISO31661Alpha3',
54
+ 'ISO4217',
55
+ 'ISO6391',
56
+ 'ISO8601',
57
+ 'ISRC',
58
+ 'ISSN',
59
+ 'javascript',
60
+ 'json',
61
+ 'JWT',
62
+ 'latLong',
63
+ 'licensePlate',
64
+ 'lowercase',
65
+ 'luhnNumber',
66
+ 'MACAddress',
67
+ 'magnetURI',
68
+ 'markdown',
69
+ 'MD5',
70
+ 'mimeType',
71
+ 'mobilePhone',
72
+ 'mobilePhoneLocales',
73
+ 'octal',
74
+ 'passportNumber',
75
+ 'port',
76
+ 'postalCode',
77
+ 'postalCodeLocales',
78
+ 'python',
79
+ 'RFC3339',
80
+ 'rgbColor',
81
+ 'rust',
82
+ 'semVer',
83
+ 'slug',
84
+ 'strongPassword',
85
+ 'surrogatePair',
86
+ 'taxID',
87
+ 'typescript',
88
+ 'uppercase',
89
+ 'URL',
90
+ 'UUID',
91
+ 'VAT',
92
+ // TODO: for discussion
93
+ 'multiline',
94
+ ];
2
95
  export const isPropType = (type, prop) => {
3
96
  return getPropType(prop) === type;
4
97
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "4.1.1",
3
+ "version": "5.0.0-alpha.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -12,7 +12,8 @@
12
12
  "scripts": {
13
13
  "build": "tsc",
14
14
  "watch": "tsc --watch",
15
- "test": "tsc && tsc test/*.ts --noEmit && tsx --test test/*.ts"
15
+ "test": "tsc && tsc test/*.ts --noEmit && tsx --test test/*.ts",
16
+ "test1": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
16
17
  },
17
18
  "prettier": "@saulx/prettier-config",
18
19
  "sideEffects": false,
@@ -22,7 +23,7 @@
22
23
  "@saulx/tsconfig": "^1.1.0",
23
24
  "@types/node": "22.5.3",
24
25
  "tsx": "^4.19.0",
25
- "typescript": "^5.1.6"
26
+ "typescript": "^5.6.3"
26
27
  },
27
28
  "dependencies": {
28
29
  "picocolors": "^1.1.0"
@@ -1,3 +0,0 @@
1
- import { Schema } from '../types.js';
2
- export declare const parsePayload: (payload: any, schema: Schema, type?: string) => void;
3
- //# sourceMappingURL=index.d.ts.map
@@ -1,2 +0,0 @@
1
- export const parsePayload = (payload, schema, type) => { };
2
- //# sourceMappingURL=index.js.map
@@ -1,6 +0,0 @@
1
- export declare const expectObject: (obj: any) => void;
2
- export declare const expectString: (obj: any) => void;
3
- export declare const expectBoolean: (v: any) => void;
4
- export declare const expectFunction: (v: any) => void;
5
- export declare const expectNumber: (v: any) => void;
6
- //# sourceMappingURL=assert.d.ts.map
@@ -1,27 +0,0 @@
1
- import { EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
2
- export const expectObject = (obj) => {
3
- if (typeof obj !== 'object' || obj === null) {
4
- throw Error(EXPECTED_OBJ);
5
- }
6
- };
7
- export const expectString = (obj) => {
8
- if (typeof obj !== 'string') {
9
- throw Error(EXPECTED_STR);
10
- }
11
- };
12
- export const expectBoolean = (v) => {
13
- if (v !== true && v !== false) {
14
- throw Error(EXPECTED_BOOL);
15
- }
16
- };
17
- export const expectFunction = (v) => {
18
- if (typeof v !== 'function') {
19
- throw Error(EXPECTED_FN);
20
- }
21
- };
22
- export const expectNumber = (v) => {
23
- if (typeof v !== 'number') {
24
- throw Error(EXPECTED_NUM);
25
- }
26
- };
27
- //# sourceMappingURL=assert.js.map
@@ -1,19 +0,0 @@
1
- export declare const OUT_OF_RANGE = "Value is out of range";
2
- export declare const EXPECTED_VALUE_IN_ENUM = "Expected value in \"enum\" field";
3
- export declare const EXPECTED_DATE = "Expected number or Date";
4
- export declare const EXPECTED_BOOL = "Expected boolean";
5
- export declare const EXPECTED_OBJ = "Expected object";
6
- export declare const EXPECTED_ARR = "Expected array";
7
- export declare const EXPECTED_NUM = "Expected number";
8
- export declare const EXPECTED_STR = "Expected string";
9
- export declare const EXPECTED_FN = "Expected function";
10
- export declare const EXPECTED_PRIMITIVE = "Expected primitive value";
11
- export declare const INVALID_VALUE = "Invalid value";
12
- export declare const INVALID_SCHEMA = "Invalid schema";
13
- export declare const MIN_MAX = "Max value must be larger than min value";
14
- export declare const UNKNOWN_PROP = "Unknown property";
15
- export declare const MISSING_TYPE = "Missing type";
16
- export declare const INVALID_TYPE = "Invalid type";
17
- export declare const TEXT_REQUIRES_LOCALES = "Type text requires locales";
18
- export declare const TYPE_MISMATCH = "Types do not match";
19
- //# sourceMappingURL=errors.d.ts.map
@@ -1,19 +0,0 @@
1
- export const OUT_OF_RANGE = 'Value is out of range';
2
- export const EXPECTED_VALUE_IN_ENUM = 'Expected value in "enum" field';
3
- export const EXPECTED_DATE = 'Expected number or Date';
4
- export const EXPECTED_BOOL = 'Expected boolean';
5
- export const EXPECTED_OBJ = 'Expected object';
6
- export const EXPECTED_ARR = 'Expected array';
7
- export const EXPECTED_NUM = 'Expected number';
8
- export const EXPECTED_STR = 'Expected string';
9
- export const EXPECTED_FN = 'Expected function';
10
- export const EXPECTED_PRIMITIVE = 'Expected primitive value';
11
- export const INVALID_VALUE = 'Invalid value';
12
- export const INVALID_SCHEMA = 'Invalid schema';
13
- export const MIN_MAX = 'Max value must be larger than min value';
14
- export const UNKNOWN_PROP = 'Unknown property';
15
- export const MISSING_TYPE = 'Missing type';
16
- export const INVALID_TYPE = 'Invalid type';
17
- export const TEXT_REQUIRES_LOCALES = 'Type text requires locales';
18
- export const TYPE_MISMATCH = 'Types do not match';
19
- //# sourceMappingURL=errors.js.map
@@ -1,20 +0,0 @@
1
- import { Schema, SchemaType } from '../types.js';
2
- import { getPropType } from './utils.js';
3
- export { getPropType };
4
- export declare class Parser {
5
- constructor(schema: Schema);
6
- inQuery: boolean;
7
- schema: Schema;
8
- type: SchemaType;
9
- parseType(type: SchemaType): void;
10
- parseTypes(): void;
11
- parseProps(props: any, schemaType?: SchemaType): void;
12
- parseLocales(): void;
13
- parse(): void;
14
- }
15
- export declare const print: (schema: Schema, path: string[]) => string;
16
- export declare const debug: (schema: Schema) => void;
17
- export declare const parseSchema: (schema: Schema) => {
18
- schema: Schema;
19
- };
20
- //# sourceMappingURL=index.d.ts.map
@@ -1,132 +0,0 @@
1
- import { INVALID_VALUE, UNKNOWN_PROP } from './errors.js';
2
- import { getPropType } from './utils.js';
3
- import propParsers from './props.js';
4
- import pc from 'picocolors';
5
- import { expectBoolean, expectObject } from './assert.js';
6
- export { getPropType };
7
- export class Parser {
8
- constructor(schema) {
9
- this.schema = schema;
10
- }
11
- inQuery;
12
- schema;
13
- type;
14
- parseType(type) {
15
- expectObject(type);
16
- this.parseProps(type.props, type);
17
- }
18
- parseTypes() {
19
- const { types } = this.schema;
20
- expectObject(types);
21
- for (const type in types) {
22
- this.parseType(types[type]);
23
- }
24
- }
25
- parseProps(props, schemaType = null) {
26
- expectObject(props);
27
- this.type = schemaType;
28
- for (const key in props) {
29
- const prop = props[key];
30
- const type = getPropType(prop);
31
- if (type in propParsers) {
32
- propParsers[type](prop, this);
33
- }
34
- else {
35
- throw Error(INVALID_VALUE);
36
- }
37
- }
38
- }
39
- parseLocales() {
40
- const { locales } = this.schema;
41
- expectObject(locales);
42
- for (const locale in locales) {
43
- const opts = locales[locale];
44
- expectObject(opts);
45
- for (const key in opts) {
46
- const val = opts[key];
47
- if (key === 'required') {
48
- expectBoolean(val);
49
- }
50
- else if (key === 'fallback') {
51
- if (!Array.isArray(val) || !val.every((v) => typeof v === 'string')) {
52
- throw Error(INVALID_VALUE);
53
- }
54
- }
55
- else {
56
- throw Error(UNKNOWN_PROP);
57
- }
58
- }
59
- }
60
- }
61
- parse() {
62
- expectObject(this.schema);
63
- for (const key in this.schema) {
64
- if (key === 'types') {
65
- this.parseTypes();
66
- }
67
- else if (key === 'props') {
68
- this.parseProps(this.schema.props);
69
- }
70
- else if (key === 'locales') {
71
- this.parseLocales();
72
- }
73
- else {
74
- throw Error(UNKNOWN_PROP);
75
- }
76
- }
77
- }
78
- }
79
- export const print = (schema, path) => {
80
- let obj = schema;
81
- const depth = path.length - 1;
82
- const lines = path.map((key, lvl) => {
83
- const v = obj[key];
84
- const padding = ' '.repeat(lvl);
85
- const prefix = key === Object.keys(obj)[0] ? '' : `${padding}...\n`;
86
- if (lvl === depth) {
87
- const err = key in obj
88
- ? `${key}: ${typeof v === 'object' && v !== null && !Array.isArray(v) ? `{..}` : JSON.stringify(v)}`
89
- : key;
90
- return `${prefix}${'--'.repeat(lvl - 1)}> ${pc.red(err)}`;
91
- }
92
- obj = v;
93
- return `${prefix}${padding}${key}: {`;
94
- });
95
- return lines.join('\n');
96
- };
97
- export const debug = (schema) => {
98
- let curr;
99
- const proxy = (obj, path = []) => {
100
- const copy = {};
101
- return new Proxy(obj, {
102
- get(_, key) {
103
- const v = obj[key];
104
- curr = [...path, key];
105
- if (typeof v !== 'object' || v === null) {
106
- return v;
107
- }
108
- copy[key] ??= proxy(obj[key], curr);
109
- return copy[key];
110
- },
111
- });
112
- };
113
- const parser = new Parser(proxy(schema));
114
- try {
115
- parser.parse();
116
- }
117
- catch (e) {
118
- e.message += '\n\n' + print(schema, curr) + '\n';
119
- e.cause = curr;
120
- throw e;
121
- }
122
- };
123
- export const parseSchema = (schema) => {
124
- try {
125
- new Parser(schema).parse();
126
- return { schema };
127
- }
128
- catch (e) {
129
- debug(schema);
130
- }
131
- };
132
- //# sourceMappingURL=index.js.map
@@ -1,7 +0,0 @@
1
- import { SchemaAnyProp } from '../types.js';
2
- import { Parser } from './index.js';
3
- type PropsFns<PropType> = Record<string, (val: any, prop: PropType, ctx: Parser, key?: string) => void>;
4
- declare function propParser<PropType extends SchemaAnyProp>(required: PropsFns<PropType>, optional: PropsFns<PropType>, allowShorthand?: number): (prop: any, ctx: Parser) => void;
5
- declare const p: Record<string, ReturnType<typeof propParser>>;
6
- export default p;
7
- //# sourceMappingURL=props.d.ts.map