@based/schema 4.1.1 → 5.0.0-alpha.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 (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 +152 -0
  5. package/dist/lang.js +149 -0
  6. package/dist/mermaid.d.ts +3 -0
  7. package/dist/mermaid.js +24 -0
  8. package/dist/parse/assert.d.ts +1 -1
  9. package/dist/parse/assert.js +2 -2
  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 +159 -75
  15. package/dist/parse/utils.d.ts +2 -2
  16. package/dist/parse/utils.js +11 -2
  17. package/dist/types.d.ts +66 -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.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.0",
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
@@ -1,256 +0,0 @@
1
- import { expectBoolean, expectFunction, expectObject, expectString, expectNumber, } from './assert.js';
2
- import { EXPECTED_ARR, EXPECTED_DATE, EXPECTED_OBJ, EXPECTED_PRIMITIVE, EXPECTED_VALUE_IN_ENUM, INVALID_VALUE, MIN_MAX, OUT_OF_RANGE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, } from './errors.js';
3
- import { getPropType } from './utils.js';
4
- const shared = {
5
- type() { },
6
- required(val) {
7
- expectBoolean(val);
8
- },
9
- query(val) {
10
- expectFunction(val);
11
- },
12
- path(val, prop, ctx) {
13
- expectString(val);
14
- const path = val.split('.');
15
- let t = ctx.type;
16
- for (const key of path) {
17
- if ('items' in t) {
18
- t = t.items;
19
- }
20
- if ('ref' in t) {
21
- t = ctx.schema.types[t.ref];
22
- }
23
- t = t.props[key];
24
- expectObject(t);
25
- }
26
- if (t.type !== getPropType(prop)) {
27
- throw Error(TYPE_MISMATCH);
28
- }
29
- },
30
- };
31
- function propParser(required, optional, allowShorthand) {
32
- return (prop, ctx) => {
33
- if (typeof prop === 'string') {
34
- // allow string
35
- if (allowShorthand === 0) {
36
- return;
37
- }
38
- throw Error(EXPECTED_OBJ);
39
- }
40
- if (Array.isArray(prop)) {
41
- // allow array
42
- if (allowShorthand === 1) {
43
- return;
44
- }
45
- throw Error(EXPECTED_OBJ);
46
- }
47
- for (const key in required) {
48
- required[key](prop[key], prop, ctx);
49
- }
50
- for (const key in prop) {
51
- const val = prop[key];
52
- if (key in optional) {
53
- optional[key](val, prop, ctx);
54
- }
55
- else if (key in shared) {
56
- shared[key](val, prop, ctx);
57
- }
58
- else if (!(key in required)) {
59
- if (key[0] === '$' && 'ref' in prop) {
60
- optional.edge(val, prop, ctx, key);
61
- }
62
- else {
63
- throw Error(UNKNOWN_PROP);
64
- }
65
- }
66
- }
67
- };
68
- }
69
- const p = {};
70
- p.boolean = propParser({}, {
71
- default(val) {
72
- expectBoolean(val);
73
- },
74
- }, 0);
75
- p.enum = propParser({
76
- enum(items) {
77
- if (!Array.isArray(items)) {
78
- throw Error(EXPECTED_ARR);
79
- }
80
- if (items.length > 255) {
81
- throw Error('Max enum length (255) exceeded');
82
- }
83
- for (const item of items) {
84
- if (typeof item === 'object') {
85
- throw Error(EXPECTED_PRIMITIVE);
86
- }
87
- }
88
- },
89
- }, {
90
- default(val, prop) {
91
- if (!prop.enum.includes(val)) {
92
- throw Error(EXPECTED_VALUE_IN_ENUM);
93
- }
94
- },
95
- }, 1);
96
- p.number = propParser({
97
- min(val) {
98
- expectNumber(val);
99
- },
100
- max(val, prop) {
101
- expectNumber(val);
102
- if (prop.min > val) {
103
- throw Error(MIN_MAX);
104
- }
105
- },
106
- step(val) {
107
- if (typeof val !== 'number' && val !== 'any') {
108
- throw Error(INVALID_VALUE);
109
- }
110
- },
111
- }, {
112
- default(val, prop) {
113
- expectNumber(val);
114
- if (val > prop.max || val < prop.min) {
115
- throw Error(OUT_OF_RANGE);
116
- }
117
- if (prop.step !== 'any') {
118
- const min = typeof prop.min !== 'number' || prop.min === Infinity ? 0 : prop.min;
119
- const v = val - min;
120
- if (~~(v / prop.step) * prop.step !== v) {
121
- throw Error(INVALID_VALUE);
122
- }
123
- }
124
- },
125
- }, 0);
126
- p.object = propParser({
127
- props(val, prop, ctx) {
128
- ctx.parseProps(val, ctx.type);
129
- },
130
- }, {
131
- default(val) {
132
- console.warn('TODO object default value');
133
- },
134
- });
135
- p.reference = propParser({
136
- ref(ref, _prop, { schema }) {
137
- schema.types[ref].props;
138
- },
139
- prop(propKey, prop, { schema, type, inQuery }) {
140
- const propAllowed = type && !inQuery;
141
- if (propAllowed) {
142
- expectString(propKey);
143
- let targetProp = schema.types[prop.ref].props[propKey];
144
- if ('items' in targetProp) {
145
- targetProp = targetProp.items;
146
- }
147
- if ('ref' in targetProp && 'prop' in targetProp) {
148
- let t = schema.types[targetProp.ref].props[targetProp.prop];
149
- if ('items' in t) {
150
- t = t.items;
151
- }
152
- if (t === prop) {
153
- return;
154
- }
155
- }
156
- throw Error(INVALID_VALUE);
157
- }
158
- if (propKey !== undefined) {
159
- throw Error('ref prop not supported on root or edge p');
160
- }
161
- },
162
- }, {
163
- default(val) {
164
- expectString(val);
165
- },
166
- edge(val, prop, ctx, key) {
167
- const edgeAllowed = ctx.type && !ctx.inQuery;
168
- if (edgeAllowed) {
169
- let t = ctx.schema.types[prop.ref].props[prop.prop];
170
- t = t.items || t;
171
- if (t[key]) {
172
- throw Error('Edge can not be defined on both props');
173
- }
174
- const edgePropType = getPropType(val);
175
- p[edgePropType](val, ctx);
176
- return;
177
- }
178
- throw Error('ref edge not supported on root or edge p');
179
- },
180
- });
181
- p.set = propParser({
182
- items(items, prop, ctx) {
183
- expectObject(items);
184
- const itemsType = getPropType(items);
185
- if (itemsType === 'string' ||
186
- itemsType === 'number' ||
187
- itemsType === 'timestamp' ||
188
- itemsType === 'boolean') {
189
- ctx.inQuery = 'query' in prop;
190
- p[itemsType](items, ctx);
191
- ctx.inQuery = false;
192
- }
193
- else {
194
- throw new Error(INVALID_VALUE);
195
- }
196
- },
197
- }, {
198
- default(val, prop) {
199
- console.warn('TODO SET DEFAULT VALUE');
200
- // if (typeof val === 'object') {
201
- // throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
202
- // }
203
- },
204
- });
205
- p.references = propParser({
206
- items(items, prop, ctx) {
207
- expectObject(items);
208
- const itemsType = getPropType(items);
209
- if (itemsType === 'reference') {
210
- ctx.inQuery = 'query' in prop;
211
- p[itemsType](items, ctx);
212
- ctx.inQuery = false;
213
- }
214
- else {
215
- throw new Error(INVALID_VALUE);
216
- }
217
- },
218
- }, {
219
- default(val, prop) {
220
- console.warn('TODO SET DEFAULT VALUE');
221
- // if (typeof val === 'object') {
222
- // throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
223
- // }
224
- },
225
- });
226
- p.string = propParser({}, {
227
- default(val) {
228
- expectString(val);
229
- },
230
- }, 0);
231
- p.text = propParser({
232
- type(_val, _prop, { schema }) {
233
- if (schema.locales) {
234
- for (const _ in schema.locales) {
235
- return;
236
- }
237
- }
238
- throw Error(TEXT_REQUIRES_LOCALES);
239
- },
240
- }, {
241
- default(val, prop) {
242
- console.warn('MAKE DEFAULT VALUE FOR TEXT');
243
- // if (typeof val !== 'string') {
244
- // throwErr(ERRORS.EXPECTED_STR, prop, 'default')
245
- // }
246
- },
247
- }, 0);
248
- p.timestamp = propParser({}, {
249
- default(val) {
250
- if (typeof val !== 'number' && !(val instanceof Date)) {
251
- throw Error(EXPECTED_DATE);
252
- }
253
- },
254
- }, 0);
255
- export default p;
256
- //# sourceMappingURL=props.js.map
@@ -1,3 +0,0 @@
1
- import { SchemaAnyProp, SchemaPropTypes } from '../types.js';
2
- export declare const getPropType: (prop: SchemaAnyProp) => SchemaPropTypes;
3
- //# sourceMappingURL=utils.d.ts.map
@@ -1,29 +0,0 @@
1
- import { INVALID_TYPE, MISSING_TYPE } from './errors.js';
2
- export const getPropType = (prop) => {
3
- if (typeof prop === 'string') {
4
- return prop;
5
- }
6
- if ('type' in prop) {
7
- if (typeof prop.type !== 'string') {
8
- throw Error(INVALID_TYPE);
9
- }
10
- return prop.type;
11
- }
12
- if ('ref' in prop) {
13
- return 'reference';
14
- }
15
- if ('items' in prop) {
16
- if (getPropType(prop.items) === 'reference') {
17
- return 'references';
18
- }
19
- return 'set';
20
- }
21
- if ('props' in prop) {
22
- return 'object';
23
- }
24
- if ('enum' in prop || Array.isArray(prop)) {
25
- return 'enum';
26
- }
27
- throw Error(MISSING_TYPE);
28
- };
29
- //# sourceMappingURL=utils.js.map
@@ -1,6 +0,0 @@
1
- import { BasedSchemaPartial } from '../types.js';
2
- import { BasedOldSchema } from './oldSchemaType.js';
3
- type OldSchema = Partial<BasedOldSchema>;
4
- export declare const convertNewToOld: (newSchema: BasedSchemaPartial) => OldSchema;
5
- export declare const convertOldToNew: (oldSchema: OldSchema) => BasedSchemaPartial;
6
- export {};