@based/schema 2.6.0 → 2.7.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 (40) hide show
  1. package/dist/src/compat/newToOld.d.ts +2 -2
  2. package/dist/src/compat/newToOld.js +172 -29
  3. package/dist/src/compat/oldToNew.d.ts +1 -1
  4. package/dist/src/compat/oldToNew.js +200 -25
  5. package/dist/src/display/number.d.ts +2 -1
  6. package/dist/src/display/number.js +10 -0
  7. package/dist/src/display/string.d.ts +3 -1
  8. package/dist/src/display/string.js +5 -0
  9. package/dist/src/display/timestamp.d.ts +3 -1
  10. package/dist/src/display/timestamp.js +9 -0
  11. package/dist/src/error.d.ts +3 -1
  12. package/dist/src/error.js +2 -0
  13. package/dist/src/index.d.ts +1 -1
  14. package/dist/src/index.js +1 -1
  15. package/dist/src/types.d.ts +4 -3
  16. package/dist/src/types.js +74 -0
  17. package/dist/src/validateSchema/basedSchemaTypeValidator.d.ts +3 -0
  18. package/dist/src/validateSchema/basedSchemaTypeValidator.js +45 -0
  19. package/dist/src/validateSchema/fieldValidators.d.ts +27 -0
  20. package/dist/src/validateSchema/fieldValidators.js +360 -0
  21. package/dist/src/validateSchema/index.d.ts +17 -0
  22. package/dist/src/validateSchema/index.js +109 -0
  23. package/dist/src/validateSchema/utils.d.ts +25 -0
  24. package/dist/src/validateSchema/utils.js +61 -0
  25. package/dist/test/compat.js +19 -3
  26. package/dist/test/data/newSchemas.d.ts +2 -2
  27. package/dist/test/data/newSchemas.js +241 -0
  28. package/dist/test/data/oldSchemas.js +122 -122
  29. package/dist/test/validateSchema/basic.js +94 -0
  30. package/dist/test/validateSchema/fields.d.ts +1 -0
  31. package/dist/test/validateSchema/fields.js +436 -0
  32. package/dist/test/validateSchema/languages.d.ts +1 -0
  33. package/dist/test/validateSchema/languages.js +124 -0
  34. package/dist/test/validateSchema/realWorld.d.ts +1 -0
  35. package/dist/test/validateSchema/realWorld.js +13 -0
  36. package/package.json +4 -3
  37. package/dist/src/validateSchema.d.ts +0 -4
  38. package/dist/src/validateSchema.js +0 -35
  39. package/dist/test/validateSchema.js +0 -38
  40. /package/dist/test/{validateSchema.d.ts → validateSchema/basic.d.ts} +0 -0
@@ -0,0 +1,61 @@
1
+ import { ParseError } from '../error.js';
2
+ export const mustBeString = (value, path) => typeof value === 'string'
3
+ ? []
4
+ : [
5
+ {
6
+ code: ParseError.incorrectFormat,
7
+ path,
8
+ },
9
+ ];
10
+ export const mustBeArray = (value, path) => Array.isArray(value)
11
+ ? []
12
+ : [
13
+ {
14
+ code: ParseError.incorrectFormat,
15
+ path,
16
+ },
17
+ ];
18
+ export const mustBeStringArray = (value, path) => Array.isArray(value) && value.every((i) => typeof i === 'string')
19
+ ? []
20
+ : [
21
+ {
22
+ code: ParseError.incorrectFormat,
23
+ path,
24
+ },
25
+ ];
26
+ export const mustBeBoolean = (value, path) => typeof value === 'boolean'
27
+ ? []
28
+ : [
29
+ {
30
+ code: ParseError.incorrectFormat,
31
+ path,
32
+ },
33
+ ];
34
+ export const mustBeNumber = (value, path) => typeof value === 'number'
35
+ ? []
36
+ : [
37
+ {
38
+ code: ParseError.incorrectFormat,
39
+ path,
40
+ },
41
+ ];
42
+ export const mustBeBidirectional = (value, path) => {
43
+ if (!(typeof value === 'object' && !Array.isArray(value))) {
44
+ return [
45
+ {
46
+ code: ParseError.incorrectFormat,
47
+ path,
48
+ },
49
+ ];
50
+ }
51
+ return value.hasOwnProperty('fromField') &&
52
+ typeof value.fromField === 'string'
53
+ ? []
54
+ : [
55
+ {
56
+ code: ParseError.incorrectFormat,
57
+ path: path.concat('fromField'),
58
+ },
59
+ ];
60
+ };
61
+ //# sourceMappingURL=utils.js.map
@@ -1,11 +1,27 @@
1
1
  import test from 'ava';
2
2
  // TODO: maybe nice to use for validate import { newSchemas } from './data/newSchemas.js'
3
+ import { newSchemas } from './data/newSchemas.js';
3
4
  import { oldSchemas } from './data/oldSchemas.js';
4
- import { convertNewToOld, convertOldToNew } from '../src/index.js';
5
+ import { convertNewToOld, convertOldToNew, validateSchema, } from '../src/index.js';
5
6
  test('old schema compat mode', async (t) => {
6
- for (let i = 0; i < oldSchemas.length; i++) {
7
+ for (let i = 0; i < oldSchemas.length - 1; i++) {
8
+ // for (let i = 0; i < 1; i++) {
7
9
  const oldSchema = oldSchemas[i];
8
- t.deepEqual(convertNewToOld(convertOldToNew(oldSchema)), oldSchema, `Schema conversion oldSchemas index ${i}`);
10
+ const newSchema = convertOldToNew(oldSchema);
11
+ const validation = await validateSchema(newSchema);
12
+ t.true(validation.valid);
13
+ t.deepEqual(oldSchema, convertNewToOld(newSchema), `Schema conversion oldSchemas index ${i}`);
14
+ }
15
+ });
16
+ test.failing('new schema compat mode', async (t) => {
17
+ for (let i = 0; i < newSchemas.length - 1; i++) {
18
+ // for (let i = 0; i < 1; i++) {
19
+ const newSchema = newSchemas[i];
20
+ const validation = await validateSchema(newSchema);
21
+ const oldSchema = convertNewToOld(newSchema);
22
+ console.dir(validation, { depth: null });
23
+ t.true(validation.valid);
24
+ t.deepEqual(newSchema, convertOldToNew(oldSchema), `Schema conversion newSchemas index ${i}`);
9
25
  }
10
26
  });
11
27
  //# sourceMappingURL=compat.js.map
@@ -1,2 +1,2 @@
1
- import { BasedSchema } from '../../src/types.js';
2
- export declare const newSchemas: BasedSchema[];
1
+ import { BasedSchemaPartial } from '../../src/types.js';
2
+ export declare const newSchemas: BasedSchemaPartial[];
@@ -1,4 +1,240 @@
1
+ const defaultFields = {
2
+ createdAt: {
3
+ readOnly: true,
4
+ type: 'timestamp',
5
+ },
6
+ updatedAt: {
7
+ readOnly: true,
8
+ type: 'timestamp',
9
+ },
10
+ };
11
+ const testSchema = {
12
+ language: 'en',
13
+ translations: ['pt', 'es'],
14
+ root: {
15
+ fields: {
16
+ currentSeason: {
17
+ type: 'reference',
18
+ title: 'Currently active season',
19
+ },
20
+ },
21
+ },
22
+ types: {
23
+ file: {
24
+ fields: {
25
+ ...defaultFields,
26
+ width: {
27
+ type: 'number',
28
+ },
29
+ height: {
30
+ type: 'number',
31
+ },
32
+ },
33
+ },
34
+ country: {
35
+ prefix: 'co',
36
+ title: 'Country',
37
+ description: 'This is the country',
38
+ fields: {
39
+ ...defaultFields,
40
+ name: {
41
+ type: 'string',
42
+ title: 'Country code',
43
+ },
44
+ title: {
45
+ type: 'text',
46
+ title: 'Translated country name',
47
+ },
48
+ circleImage: {
49
+ type: 'reference',
50
+ title: 'Circular flag image',
51
+ },
52
+ heartImage: {
53
+ type: 'reference',
54
+ title: 'Heart-shaped flag image',
55
+ },
56
+ },
57
+ },
58
+ season: {
59
+ prefix: 'se',
60
+ fields: {
61
+ ...defaultFields,
62
+ name: {
63
+ type: 'string',
64
+ title: 'Season name',
65
+ },
66
+ winner: {
67
+ type: 'reference',
68
+ allowedTypes: ['contestant'],
69
+ },
70
+ countries: {
71
+ type: 'references',
72
+ title: 'Participating countries',
73
+ },
74
+ },
75
+ },
76
+ episode: {
77
+ prefix: 'ep',
78
+ fields: {
79
+ ...defaultFields,
80
+ episodeType: {
81
+ type: 'string', // redemption, grandFinal, nationalFinal or nationalQualifier
82
+ title: 'Type of episode',
83
+ },
84
+ title: {
85
+ type: 'text',
86
+ title: 'Episode title',
87
+ },
88
+ image: {
89
+ type: 'reference',
90
+ allowedTypes: ['file'],
91
+ },
92
+ startTime: {
93
+ type: 'timestamp',
94
+ title: 'Start time',
95
+ },
96
+ endTime: {
97
+ type: 'timestamp',
98
+ title: 'End time',
99
+ },
100
+ producer: {
101
+ type: 'reference',
102
+ allowedTypes: ['broadcaster'],
103
+ },
104
+ distributors: {
105
+ type: 'references',
106
+ allowedTypes: ['broadcaster'],
107
+ },
108
+ },
109
+ },
110
+ votingWindow: {
111
+ prefix: 'vo',
112
+ fields: {
113
+ ...defaultFields,
114
+ startTime: {
115
+ type: 'timestamp',
116
+ },
117
+ closeTime: {
118
+ type: 'timestamp',
119
+ },
120
+ endTime: {
121
+ type: 'timestamp',
122
+ },
123
+ },
124
+ },
125
+ contestant: {
126
+ prefix: 'ct',
127
+ fields: {
128
+ ...defaultFields,
129
+ name: {
130
+ type: 'string',
131
+ },
132
+ song: {
133
+ type: 'string',
134
+ },
135
+ songUrl: {
136
+ type: 'string',
137
+ format: 'URL',
138
+ },
139
+ writer: {
140
+ type: 'string',
141
+ },
142
+ image: {
143
+ type: 'reference',
144
+ allowedTypes: ['file'],
145
+ },
146
+ content: {
147
+ type: 'text',
148
+ format: 'html',
149
+ },
150
+ },
151
+ },
152
+ broadcaster: {
153
+ prefix: 'br',
154
+ fields: {
155
+ ...defaultFields,
156
+ name: {
157
+ type: 'string',
158
+ },
159
+ email: {
160
+ type: 'string',
161
+ format: 'email',
162
+ },
163
+ image: {
164
+ type: 'reference',
165
+ allowedTypes: ['file'],
166
+ },
167
+ },
168
+ },
169
+ feed: {
170
+ prefix: 'fe',
171
+ fields: {
172
+ ...defaultFields,
173
+ title: {
174
+ type: 'text',
175
+ },
176
+ content: {
177
+ type: 'text',
178
+ format: 'html',
179
+ },
180
+ imageUrl: {
181
+ type: 'string',
182
+ format: 'URL',
183
+ },
184
+ videoUrl: {
185
+ type: 'string',
186
+ format: 'URL',
187
+ },
188
+ url: {
189
+ type: 'string',
190
+ format: 'URL',
191
+ },
192
+ },
193
+ },
194
+ user: {
195
+ prefix: 'us',
196
+ fields: {
197
+ ...defaultFields,
198
+ name: { type: 'string' },
199
+ language: { type: 'string' },
200
+ notify: { type: 'boolean' },
201
+ country: { type: 'string' },
202
+ customerId: {
203
+ // stripe customerId
204
+ type: 'string',
205
+ },
206
+ credits: {
207
+ type: 'record',
208
+ values: {
209
+ type: 'object',
210
+ properties: {
211
+ purchased: {
212
+ type: 'number',
213
+ },
214
+ earned: {
215
+ type: 'number',
216
+ },
217
+ },
218
+ },
219
+ },
220
+ voteHistory: {
221
+ type: 'record',
222
+ values: {
223
+ type: 'record',
224
+ values: {
225
+ type: 'record',
226
+ values: {
227
+ type: 'number',
228
+ },
229
+ },
230
+ },
231
+ },
232
+ },
233
+ },
234
+ },
235
+ };
1
236
  export const newSchemas = [
237
+ testSchema,
2
238
  {
3
239
  types: {
4
240
  thing: {
@@ -11,6 +247,10 @@ export const newSchemas = [
11
247
  bla: {
12
248
  prefix: 'bl',
13
249
  fields: {
250
+ createdAt: {
251
+ type: 'timestamp',
252
+ readOnly: true,
253
+ },
14
254
  enum: {
15
255
  enum: ['tony', 'jim'],
16
256
  },
@@ -109,6 +349,7 @@ export const newSchemas = [
109
349
  },
110
350
  $defs: {},
111
351
  language: 'en',
352
+ translations: [],
112
353
  root: {
113
354
  fields: {},
114
355
  },