@forgehive/schema 0.1.3 → 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.
@@ -1,4 +1,4 @@
1
- import Schema, { type SchemaDescription } from '../index'
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 create a schema from description with string record type', () => {
38
- const description: SchemaDescription = {
39
- data: {
40
- type: 'stringRecord',
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
- const result = schema.validate(validData)
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 create a schema from description with number record type', () => {
93
- const description: SchemaDescription = {
94
- data: {
95
- type: 'numberRecord',
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
- const result = schema.validate(validData)
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 create a schema from description with boolean record type', () => {
148
- const description: SchemaDescription = {
149
- data: {
150
- type: 'booleanRecord',
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
- const result = schema.validate(validData)
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 create a schema from description with mixed record type', () => {
251
- const description: SchemaDescription = {
252
- data: {
253
- type: 'mixedRecord',
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
- const result = schema.validate(validData)
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 reject a record with non-string keys', () => {
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 create a schema from description with optional record type', () => {
300
- const description: SchemaDescription = {
301
- data: {
302
- type: 'stringRecord',
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 schema with string record type', () => {
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
- expect(description).toHaveProperty('data')
331
- expect(description.data).toHaveProperty('type', 'stringRecord')
332
- // The optional property is only included when it's true, not when it's false
300
+ expect(description.properties?.data).toMatchObject({
301
+ type: 'object',
302
+ additionalProperties: { type: 'string' }
303
+ })
333
304
  })
334
305
 
335
- it('should describe a schema with number record type', () => {
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
- expect(description).toHaveProperty('data')
343
- expect(description.data).toHaveProperty('type', 'numberRecord')
312
+ expect(description.properties?.data).toMatchObject({
313
+ type: 'object',
314
+ additionalProperties: { type: 'number' }
315
+ })
344
316
  })
345
317
 
346
- it('should describe a schema with boolean record type', () => {
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
- expect(description).toHaveProperty('data')
354
- expect(description.data).toHaveProperty('type', 'booleanRecord')
324
+ expect(description.properties?.data).toMatchObject({
325
+ type: 'object',
326
+ additionalProperties: { type: 'boolean' }
327
+ })
355
328
  })
356
329
 
357
- it('should describe a schema with mixed record type', () => {
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
- expect(description).toHaveProperty('data')
365
- expect(description.data).toHaveProperty('type', 'mixedRecord')
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": "es2015",
12
- "lib": ["es2015", "dom"]
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
-