Skip to content

Commit 73e7acc

Browse files
Copilotstreamich
andcommitted
Update MapSchema and ObjectFieldSchema to use value instead of type
Co-authored-by: streamich <[email protected]>
1 parent e466a23 commit 73e7acc

File tree

15 files changed

+107
-101
lines changed

15 files changed

+107
-101
lines changed

src/jtd/converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function toJtdForm(schema: schema.Schema): jtd.JtdForm {
7777

7878
for (const field of objSchema.fields) {
7979
const fieldName = field.key;
80-
const fieldType = field.type;
80+
const fieldType = field.value;
8181

8282
if (fieldType) {
8383
const fieldJtd = toJtdForm(fieldType);
@@ -107,7 +107,7 @@ export function toJtdForm(schema: schema.Schema): jtd.JtdForm {
107107
case 'map': {
108108
const mapSchema = schema as schema.MapSchema;
109109
return {
110-
values: toJtdForm(mapSchema.type),
110+
values: toJtdForm(mapSchema.value),
111111
};
112112
}
113113
case 'ref': {

src/schema/SchemaBuilder.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,27 @@ export class SchemaBuilder {
176176
/** @deprecated Use `.prop`. */
177177
public Field<K extends string, V extends Schema>(
178178
key: K,
179-
type: V,
180-
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'type' | 'optional'> = {},
179+
value: V,
180+
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'value' | 'optional'> = {},
181181
): ObjectFieldSchema<K, V> {
182182
return {
183183
kind: 'field',
184184
key,
185-
type,
185+
value,
186186
...options,
187187
};
188188
}
189189

190190
/** @deprecated Use `.propOpt`. */
191191
public FieldOpt<K extends string, V extends Schema>(
192192
key: K,
193-
type: V,
194-
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'type' | 'optional'> = {},
193+
value: V,
194+
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'value' | 'optional'> = {},
195195
): ObjectOptionalFieldSchema<K, V> {
196196
return {
197197
kind: 'field',
198198
key,
199-
type,
199+
value,
200200
...options,
201201
optional: true,
202202
};
@@ -205,34 +205,38 @@ export class SchemaBuilder {
205205
/** Declares an object property. */
206206
public prop<K extends string, V extends Schema>(
207207
key: K,
208-
type: V,
209-
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'type' | 'optional'> = {},
208+
value: V,
209+
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'value' | 'optional'> = {},
210210
): ObjectFieldSchema<K, V> {
211211
return {
212212
kind: 'field',
213213
key,
214-
type,
214+
value,
215215
...options,
216216
};
217217
}
218218

219219
/** Declares an optional object property. */
220220
public propOpt<K extends string, V extends Schema>(
221221
key: K,
222-
type: V,
223-
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'type' | 'optional'> = {},
222+
value: V,
223+
options: Omit<NoT<ObjectFieldSchema<K, V>>, 'key' | 'value' | 'optional'> = {},
224224
): ObjectOptionalFieldSchema<K, V> {
225225
return {
226226
kind: 'field',
227227
key,
228-
type,
228+
value,
229229
...options,
230230
optional: true,
231231
};
232232
}
233233

234-
public Map<T extends Schema>(type: T, options?: Omit<NoT<MapSchema<T>>, 'type'>): MapSchema<T> {
235-
return {kind: 'map', type, ...options};
234+
public Map<V extends Schema, K extends Schema = StringSchema>(
235+
value: V,
236+
key?: K,
237+
options?: Omit<NoT<MapSchema<V, K>>, 'value' | 'key'>
238+
): MapSchema<V, K> {
239+
return {kind: 'map', value, ...(key && {key}), ...options};
236240
}
237241

238242
public Any(options: NoT<AnySchema> = {}): AnySchema {

src/schema/__tests__/SchemaBuilder.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ describe('object', () => {
4242
{
4343
kind: 'field',
4444
key: 'id',
45-
type: {
45+
value: {
4646
kind: 'str',
4747
id: 'UserId',
4848
},
4949
},
5050
{
5151
kind: 'field',
5252
key: 'name',
53-
type: {
53+
value: {
5454
kind: 'str',
5555
},
5656
},
@@ -61,11 +61,11 @@ describe('object', () => {
6161

6262
describe('map', () => {
6363
test('can create an simple object using shorthand', () => {
64-
expect(s.map).toEqual({kind: 'map', type: {kind: 'any'}});
64+
expect(s.map).toEqual({kind: 'map', value: {kind: 'any'}});
6565
});
6666

6767
test('can define a map', () => {
68-
expect(s.Map(s.Boolean())).toEqual({kind: 'map', type: {kind: 'bool'}});
68+
expect(s.Map(s.Boolean())).toEqual({kind: 'map', value: {kind: 'bool'}});
6969
});
7070
});
7171

src/schema/__tests__/type.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@ test('can generate any type', () => {
2222
fields: [
2323
{
2424
key: 'id',
25-
type: {
25+
value: {
2626
kind: 'num',
2727
format: 'i',
2828
},
2929
},
3030
{
3131
key: 'alwaysOne',
32-
type: {
32+
value: {
3333
kind: 'con',
3434
value: 1,
3535
},
3636
},
3737
{
3838
key: 'name',
39-
type: {
39+
value: {
4040
kind: 'str',
4141
},
4242
},
4343
{
4444
key: 'address',
45-
type: {
45+
value: {
4646
kind: 'obj',
4747
title: 'User address',
4848
description: 'Various address fields for user',
4949
fields: [
5050
{
5151
key: 'street',
52-
type: {
52+
value: {
5353
kind: 'str',
5454
},
5555
},
5656
{
5757
key: 'zip',
58-
type: {
58+
value: {
5959
kind: 'str',
6060
},
6161
},
@@ -64,15 +64,15 @@ test('can generate any type', () => {
6464
},
6565
{
6666
key: 'timeCreated',
67-
type: {
67+
value: {
6868
kind: 'num',
6969
},
7070
},
7171
{
7272
key: 'tags',
73-
type: {
73+
value: {
7474
kind: 'arr',
75-
type: {
75+
value: {
7676
kind: 'or',
7777
types: [
7878
{
@@ -87,9 +87,9 @@ test('can generate any type', () => {
8787
},
8888
{
8989
key: 'elements',
90-
type: {
90+
value: {
9191
kind: 'map',
92-
type: {
92+
value: {
9393
kind: 'str',
9494
},
9595
},

src/schema/__tests__/validate.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('validateSchema', () => {
319319
test('validates valid binary schema', () => {
320320
const schema: Schema = {
321321
kind: 'bin',
322-
type: {kind: 'str'},
322+
value: {kind: 'str'},
323323
};
324324
expect(() => validateSchema(schema)).not.toThrow();
325325
});
@@ -329,7 +329,7 @@ describe('validateSchema', () => {
329329
for (const format of formats) {
330330
const schema: Schema = {
331331
kind: 'bin',
332-
type: {kind: 'str'},
332+
value: {kind: 'str'},
333333
format,
334334
};
335335
expect(() => validateSchema(schema)).not.toThrow();
@@ -340,7 +340,7 @@ describe('validateSchema', () => {
340340
expect(() =>
341341
validateSchema({
342342
kind: 'bin',
343-
type: {kind: 'str'},
343+
value: {kind: 'str'},
344344
format: 'invalid',
345345
} as any),
346346
).toThrow('FORMAT');
@@ -351,15 +351,15 @@ describe('validateSchema', () => {
351351
test('validates valid array schema', () => {
352352
const schema: Schema = {
353353
kind: 'arr',
354-
type: {kind: 'str'},
354+
value: {kind: 'str'},
355355
};
356356
expect(() => validateSchema(schema)).not.toThrow();
357357
});
358358

359359
test('validates array schema with constraints', () => {
360360
const schema: Schema = {
361361
kind: 'arr',
362-
type: {kind: 'num'},
362+
value: {kind: 'num'},
363363
min: 1,
364364
max: 10,
365365
};
@@ -396,7 +396,7 @@ describe('validateSchema', () => {
396396
{
397397
kind: 'field',
398398
key: 'name',
399-
type: {kind: 'str'},
399+
value: {kind: 'str'},
400400
},
401401
],
402402
};
@@ -428,7 +428,7 @@ describe('validateSchema', () => {
428428
const schema: Schema = {
429429
kind: 'field',
430430
key: 'test',
431-
type: {kind: 'str'},
431+
value: {kind: 'str'},
432432
};
433433
expect(() => validateSchema(schema)).not.toThrow();
434434
});
@@ -437,7 +437,7 @@ describe('validateSchema', () => {
437437
const schema: Schema = {
438438
kind: 'field',
439439
key: 'test',
440-
type: {kind: 'str'},
440+
value: {kind: 'str'},
441441
optional: true,
442442
};
443443
expect(() => validateSchema(schema)).not.toThrow();
@@ -448,7 +448,7 @@ describe('validateSchema', () => {
448448
validateSchema({
449449
kind: 'field',
450450
key: 123,
451-
type: {kind: 'str'},
451+
value: {kind: 'str'},
452452
} as any),
453453
).toThrow('KEY_TYPE');
454454
});
@@ -458,7 +458,7 @@ describe('validateSchema', () => {
458458
validateSchema({
459459
kind: 'field',
460460
key: 'test',
461-
type: {kind: 'str'},
461+
value: {kind: 'str'},
462462
optional: 'true',
463463
} as any),
464464
).toThrow('OPTIONAL_TYPE');
@@ -469,7 +469,7 @@ describe('validateSchema', () => {
469469
test('validates valid map schema', () => {
470470
const schema: Schema = {
471471
kind: 'map',
472-
type: {kind: 'str'},
472+
value: {kind: 'str'},
473473
};
474474
expect(() => validateSchema(schema)).not.toThrow();
475475
});

src/schema/schema.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,9 @@ export interface ObjectFieldSchema<K extends string = string, V extends TType =
358358
key: K;
359359

360360
/**
361-
* One or more "one-of" types of the field.
362-
*
363-
* @todo Rename to `val`.
361+
* Type of the field value.
364362
*/
365-
type: V;
363+
value: V;
366364

367365
optional?: boolean;
368366
}
@@ -376,14 +374,16 @@ export interface ObjectOptionalFieldSchema<K extends string = string, V extends
376374
* Represents an object, which is treated as a map. All keys are strings and all
377375
* values are of the same type.
378376
*/
379-
export interface MapSchema<T extends TType = any> extends TType<Record<string, unknown>>, WithValidator {
377+
export interface MapSchema<V extends TType = any, K extends TType = any> extends TType<Record<string, unknown>>, WithValidator {
380378
kind: 'map';
379+
/**
380+
* Type of all keys in the map. Defaults to string type.
381+
*/
382+
key?: K;
381383
/**
382384
* Type of all values in the map.
383-
*
384-
* @todo Rename to `val`. And add `key` field for the key type. Make `key` default to `str`.
385385
*/
386-
type: T;
386+
value: V;
387387
}
388388

389389
/**

src/type/TypeBuilder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ export class TypeBuilder {
228228
return field;
229229
}
230230

231-
public Map<T extends Type>(val: T, options?: schema.Optional<schema.MapSchema>) {
232-
const map = new classes.MapType<T>(val, options);
231+
public Map<T extends Type>(val: T, key?: Type, options?: schema.Optional<schema.MapSchema>) {
232+
const map = new classes.MapType<T>(val, key, options);
233233
map.system = this.system;
234234
return map;
235235
}
@@ -286,13 +286,13 @@ export class TypeBuilder {
286286
return this.Object(
287287
...node.fields.map((f: any) =>
288288
f.optional
289-
? this.propOpt(f.key, this.import(f.type)).options(f)
290-
: this.prop(f.key, this.import(f.type)).options(f),
289+
? this.propOpt(f.key, this.import(f.value)).options(f)
290+
: this.prop(f.key, this.import(f.value)).options(f),
291291
),
292292
).options(node);
293293
}
294294
case 'map':
295-
return this.Map(this.import(node.type), node);
295+
return this.Map(this.import(node.value), node.key ? this.import(node.key) : undefined, node);
296296
case 'con':
297297
return this.Const(node.value).options(node);
298298
case 'or':

0 commit comments

Comments
 (0)