@forgehive/schema 0.1.4 → 0.2.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.
- package/README.md +105 -59
- package/dist/index.d.ts +74 -71
- package/dist/index.js +68 -182
- package/dist/test/custom-validations.test.js +45 -131
- package/dist/test/dates.test.js +41 -21
- package/dist/test/describe.test.d.ts +1 -0
- package/dist/test/describe.test.js +172 -0
- package/dist/test/from.test.d.ts +1 -0
- package/dist/test/from.test.js +162 -0
- package/dist/test/index.test.js +130 -30
- package/dist/test/optionals.test.js +22 -18
- package/dist/test/parse.test.d.ts +1 -0
- package/dist/test/parse.test.js +134 -0
- package/dist/test/record.test.js +47 -62
- package/package.json +2 -2
- package/src/index.ts +105 -251
- package/src/test/custom-validations.test.ts +45 -132
- package/src/test/dates.test.ts +45 -22
- package/src/test/describe.test.ts +190 -0
- package/src/test/from.test.ts +191 -0
- package/src/test/index.test.ts +148 -31
- package/src/test/optionals.test.ts +23 -18
- package/src/test/parse.test.ts +160 -0
- package/src/test/record.test.ts +48 -72
- package/tsconfig.json +2 -3
package/src/test/record.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Schema
|
|
1
|
+
import Schema from '../index'
|
|
2
2
|
|
|
3
3
|
describe('Schema Record Types', () => {
|
|
4
4
|
describe('String Record', () => {
|
|
@@ -34,15 +34,10 @@ describe('Schema Record Types', () => {
|
|
|
34
34
|
expect(result).toBe(false)
|
|
35
35
|
})
|
|
36
36
|
|
|
37
|
-
it('should
|
|
38
|
-
const
|
|
39
|
-
data:
|
|
40
|
-
|
|
41
|
-
optional: false
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const schema = Schema.from(description)
|
|
37
|
+
it('should round-trip a string record schema', () => {
|
|
38
|
+
const schema = Schema.from(new Schema({
|
|
39
|
+
data: Schema.stringRecord(),
|
|
40
|
+
}).describe())
|
|
46
41
|
|
|
47
42
|
const validData = {
|
|
48
43
|
data: {
|
|
@@ -51,8 +46,7 @@ describe('Schema Record Types', () => {
|
|
|
51
46
|
}
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
expect(result).toBe(true)
|
|
49
|
+
expect(schema.validate(validData)).toBe(true)
|
|
56
50
|
})
|
|
57
51
|
})
|
|
58
52
|
|
|
@@ -89,15 +83,10 @@ describe('Schema Record Types', () => {
|
|
|
89
83
|
expect(result).toBe(false)
|
|
90
84
|
})
|
|
91
85
|
|
|
92
|
-
it('should
|
|
93
|
-
const
|
|
94
|
-
data:
|
|
95
|
-
|
|
96
|
-
optional: false
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const schema = Schema.from(description)
|
|
86
|
+
it('should round-trip a number record schema', () => {
|
|
87
|
+
const schema = Schema.from(new Schema({
|
|
88
|
+
data: Schema.numberRecord(),
|
|
89
|
+
}).describe())
|
|
101
90
|
|
|
102
91
|
const validData = {
|
|
103
92
|
data: {
|
|
@@ -106,8 +95,7 @@ describe('Schema Record Types', () => {
|
|
|
106
95
|
}
|
|
107
96
|
}
|
|
108
97
|
|
|
109
|
-
|
|
110
|
-
expect(result).toBe(true)
|
|
98
|
+
expect(schema.validate(validData)).toBe(true)
|
|
111
99
|
})
|
|
112
100
|
})
|
|
113
101
|
|
|
@@ -144,15 +132,10 @@ describe('Schema Record Types', () => {
|
|
|
144
132
|
expect(result).toBe(false)
|
|
145
133
|
})
|
|
146
134
|
|
|
147
|
-
it('should
|
|
148
|
-
const
|
|
149
|
-
data:
|
|
150
|
-
|
|
151
|
-
optional: false
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const schema = Schema.from(description)
|
|
135
|
+
it('should round-trip a boolean record schema', () => {
|
|
136
|
+
const schema = Schema.from(new Schema({
|
|
137
|
+
data: Schema.booleanRecord(),
|
|
138
|
+
}).describe())
|
|
156
139
|
|
|
157
140
|
const validData = {
|
|
158
141
|
data: {
|
|
@@ -161,8 +144,7 @@ describe('Schema Record Types', () => {
|
|
|
161
144
|
}
|
|
162
145
|
}
|
|
163
146
|
|
|
164
|
-
|
|
165
|
-
expect(result).toBe(true)
|
|
147
|
+
expect(schema.validate(validData)).toBe(true)
|
|
166
148
|
})
|
|
167
149
|
})
|
|
168
150
|
|
|
@@ -247,15 +229,10 @@ describe('Schema Record Types', () => {
|
|
|
247
229
|
expect(result).toBe(false)
|
|
248
230
|
})
|
|
249
231
|
|
|
250
|
-
it('should
|
|
251
|
-
const
|
|
252
|
-
data:
|
|
253
|
-
|
|
254
|
-
optional: false
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const schema = Schema.from(description)
|
|
232
|
+
it('should round-trip a mixed record schema', () => {
|
|
233
|
+
const schema = Schema.from(new Schema({
|
|
234
|
+
data: Schema.mixedRecord(),
|
|
235
|
+
}).describe())
|
|
259
236
|
|
|
260
237
|
const validData = {
|
|
261
238
|
data: {
|
|
@@ -265,13 +242,12 @@ describe('Schema Record Types', () => {
|
|
|
265
242
|
}
|
|
266
243
|
}
|
|
267
244
|
|
|
268
|
-
|
|
269
|
-
expect(result).toBe(true)
|
|
245
|
+
expect(schema.validate(validData)).toBe(true)
|
|
270
246
|
})
|
|
271
247
|
})
|
|
272
248
|
|
|
273
249
|
describe('Common Record Functionality', () => {
|
|
274
|
-
it('should
|
|
250
|
+
it('should treat numeric keys as strings (JS limitation)', () => {
|
|
275
251
|
const schema = new Schema({
|
|
276
252
|
data: Schema.stringRecord(),
|
|
277
253
|
})
|
|
@@ -296,15 +272,10 @@ describe('Schema Record Types', () => {
|
|
|
296
272
|
expect(result).toBe(true)
|
|
297
273
|
})
|
|
298
274
|
|
|
299
|
-
it('should
|
|
300
|
-
const
|
|
301
|
-
data:
|
|
302
|
-
|
|
303
|
-
optional: true
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const schema = Schema.from(description)
|
|
275
|
+
it('should round-trip an optional record schema', () => {
|
|
276
|
+
const schema = Schema.from(new Schema({
|
|
277
|
+
data: Schema.stringRecord().optional()
|
|
278
|
+
}).describe())
|
|
308
279
|
|
|
309
280
|
// Test with record present
|
|
310
281
|
const validData1 = {
|
|
@@ -320,49 +291,54 @@ describe('Schema Record Types', () => {
|
|
|
320
291
|
expect(schema.validate(validData2)).toBe(true)
|
|
321
292
|
})
|
|
322
293
|
|
|
323
|
-
it('should describe a
|
|
294
|
+
it('should describe a string record as an object with string additionalProperties', () => {
|
|
324
295
|
const schema = new Schema({
|
|
325
296
|
data: Schema.stringRecord(),
|
|
326
297
|
})
|
|
327
298
|
|
|
328
299
|
const description = schema.describe()
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
300
|
+
expect(description.properties?.data).toMatchObject({
|
|
301
|
+
type: 'object',
|
|
302
|
+
additionalProperties: { type: 'string' }
|
|
303
|
+
})
|
|
333
304
|
})
|
|
334
305
|
|
|
335
|
-
it('should describe a
|
|
306
|
+
it('should describe a number record as an object with number additionalProperties', () => {
|
|
336
307
|
const schema = new Schema({
|
|
337
308
|
data: Schema.numberRecord(),
|
|
338
309
|
})
|
|
339
310
|
|
|
340
311
|
const description = schema.describe()
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
312
|
+
expect(description.properties?.data).toMatchObject({
|
|
313
|
+
type: 'object',
|
|
314
|
+
additionalProperties: { type: 'number' }
|
|
315
|
+
})
|
|
344
316
|
})
|
|
345
317
|
|
|
346
|
-
it('should describe a
|
|
318
|
+
it('should describe a boolean record as an object with boolean additionalProperties', () => {
|
|
347
319
|
const schema = new Schema({
|
|
348
320
|
data: Schema.booleanRecord(),
|
|
349
321
|
})
|
|
350
322
|
|
|
351
323
|
const description = schema.describe()
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
324
|
+
expect(description.properties?.data).toMatchObject({
|
|
325
|
+
type: 'object',
|
|
326
|
+
additionalProperties: { type: 'boolean' }
|
|
327
|
+
})
|
|
355
328
|
})
|
|
356
329
|
|
|
357
|
-
it('should describe a
|
|
330
|
+
it('should describe a mixed record as an object with anyOf additionalProperties', () => {
|
|
358
331
|
const schema = new Schema({
|
|
359
332
|
data: Schema.mixedRecord(),
|
|
360
333
|
})
|
|
361
334
|
|
|
362
335
|
const description = schema.describe()
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
336
|
+
expect(description.properties?.data).toMatchObject({
|
|
337
|
+
type: 'object',
|
|
338
|
+
additionalProperties: {
|
|
339
|
+
anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }]
|
|
340
|
+
}
|
|
341
|
+
})
|
|
366
342
|
})
|
|
367
343
|
})
|
|
368
344
|
})
|
package/tsconfig.json
CHANGED
|
@@ -8,11 +8,10 @@
|
|
|
8
8
|
"module": "commonjs",
|
|
9
9
|
"moduleResolution": "node",
|
|
10
10
|
"declaration": true,
|
|
11
|
-
"target": "
|
|
12
|
-
"lib": ["
|
|
11
|
+
"target": "es2020",
|
|
12
|
+
"lib": ["es2020", "dom"]
|
|
13
13
|
},
|
|
14
14
|
"files": ["src/index.ts"],
|
|
15
15
|
"include": ["src/**/*.ts", "src/**/*.json"],
|
|
16
16
|
"exclude": ["node_modules", "dist/*"]
|
|
17
17
|
}
|
|
18
|
-
|