Skip to content

Commit 5b92e12

Browse files
authored
Merge pull request #32 from jsonjoy-com/nested-references
Cleanup
2 parents 28e2f81 + dc93c92 commit 5b92e12

File tree

9 files changed

+38
-195
lines changed

9 files changed

+38
-195
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @module
3+
* @todo Move to `src/validator/`.
4+
*/
5+
16
/**
27
* Validation error codes.
38
*

src/random/generators.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
22
import {cloneBinary} from '@jsonjoy.com/util/lib/json-clone';
3-
import type {AbstractType} from '../type/classes/AbstractType';
43
import type {AnyType} from '../type/classes/AnyType';
54
import type {ArrayType} from '../type/classes/ArrayType';
65
import type {BinaryType} from '../type/classes/BinaryType';

src/system/Method.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/system/TypeRouter.ts

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/system/__tests__/TypeSystem.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ describe('.toString()', () => {
4242
4343
└─ validators
4444
└─ "empty-string""
45+
`);
46+
});
47+
48+
test('prints type with nested self reference', () => {
49+
const system = new TypeSystem();
50+
const {t} = system;
51+
system.alias('User', t.obj.prop('id', t.str).opt('friend', t.Ref('User')));
52+
expect(system.toString()).toMatchInlineSnapshot(`
53+
"TypeSystem
54+
├─ aliases
55+
│ └─ User
56+
│ └─ obj
57+
│ ├─ "id":
58+
│ │ └─ str
59+
│ └─ "friend"?:
60+
│ └─ ref → [User]
61+
└─ validators"
4562
`);
4663
});
4764
});

src/system/__tests__/toTypeScript.spec.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {TypeSystem} from '..';
2-
import {TypeRouter} from '../TypeRouter';
32

43
test('generates TypeScript source for simple string type', () => {
54
const system = new TypeSystem();
@@ -92,49 +91,3 @@ test('type interface inside a tuple', () => {
9291
"
9392
`);
9493
});
95-
96-
test('can export whole router', () => {
97-
const system = new TypeSystem();
98-
const {t} = system;
99-
const router = new TypeRouter({system, routes: {}}).extend(() => ({
100-
callMe: t.Function(t.str, t.num),
101-
'block.subscribe': t.Function$(t.Object(t.prop('id', t.str)), t.obj),
102-
}));
103-
expect(router.toTypeScript()).toMatchInlineSnapshot(`
104-
"export namespace Router {
105-
export type Routes = {
106-
callMe: (request: string) => Promise<number>;
107-
"block.subscribe": (request$: Observable<{
108-
id: string;
109-
}>) => Observable<{}>;
110-
};
111-
}
112-
"
113-
`);
114-
});
115-
116-
test('can export whole router and aliases', () => {
117-
const system = new TypeSystem();
118-
const {t} = system;
119-
system.alias('Document', t.Object(t.prop('id', t.str), t.prop('title', t.str)).options({title: 'The document'}));
120-
const router = new TypeRouter({system, routes: {}}).extend(() => ({
121-
callMe: t.Function(t.str, t.num),
122-
'block.subscribe': t.Function$(t.Object(t.prop('id', t.str)), t.Ref('Document')),
123-
}));
124-
expect(router.toTypeScript()).toMatchInlineSnapshot(`
125-
"export namespace Router {
126-
export type Routes = {
127-
callMe: (request: string) => Promise<number>;
128-
"block.subscribe": (request$: Observable<{
129-
id: string;
130-
}>) => Observable<Document>;
131-
};
132-
133-
export interface Document {
134-
id: string;
135-
title: string;
136-
}
137-
}
138-
"
139-
`);
140-
});

src/type/__tests__/validateTestSuite.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {type Type, t} from '..';
2+
import {ValidationError, ValidationErrorMessage} from '../../constants';
23
import {TypeSystem} from '../../system/TypeSystem';
34

45
export const validateTestSuite = (validate: (type: Type, value: unknown) => void) => {
@@ -384,6 +385,21 @@ export const validateTestSuite = (validate: (type: Type, value: unknown) => void
384385
expect(() => validate(type, [1])).toThrowErrorMatchingInlineSnapshot(`"STR"`);
385386
});
386387

388+
test('object nested in array', () => {
389+
const type = t.obj.prop('foo', t.array(t.obj.prop('bar', t.str)));
390+
validate(type, {foo: [{bar: 'baz'}]});
391+
const validator = type.validator('object');
392+
const res = validator({foo: [{bar: 'baz'}]});
393+
expect(res).toBe(null);
394+
const res2 = validator({foo: {bar: 'baz'}});
395+
expect(res2).toEqual({
396+
code: ValidationError[ValidationError.ARR],
397+
errno: ValidationError.ARR,
398+
message: ValidationErrorMessage[ValidationError.ARR],
399+
path: ['foo'],
400+
});
401+
});
402+
387403
describe('size bounds', () => {
388404
test('respects min and max', () => {
389405
const type = t.arr.options({min: 2, max: 4});

src/type/classes/ArrayType.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {BinaryEncoderCodegenContext} from '../../codegen/binary/BinaryEncod
1414
import type {SchemaOf, Type} from '../types';
1515
import type {TypeSystem} from '../../system/TypeSystem';
1616
import type {json_string} from '@jsonjoy.com/util/lib/json-brand';
17-
import type * as ts from '../../typescript/types';
1817
import type {TypeExportContext} from '../../system/TypeExportContext';
1918

2019
export class ArrayType<T extends Type> extends AbstractType<schema.ArraySchema<SchemaOf<T>>> {

src/value/ObjectValue.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ export type ObjectValueToTypeMap<F> = ToObject<{
1818
}>;
1919
export type TuplesToFields<T> = T extends PropDefinition<infer K, infer V>[] ? classes.ObjectFieldType<K, V>[] : never;
2020

21-
// export type MergeObjectsTypes<A, B> =
22-
// A extends classes.ObjectType<infer A2>
23-
// ? B extends classes.ObjectType<infer B2>
24-
// ? classes.ObjectType<[...A2, ...B2]> :
25-
// never :
26-
// never;
27-
28-
// export type MergeObjectValues<A, B> =
29-
// A extends ObjectValue<infer A2>
30-
// ? B extends ObjectValue<infer B2>
31-
// ? ObjectValue<MergeObjectsTypes<A2, B2>> :
32-
// never :
33-
// never;
34-
3521
type PropDefinition<K extends string, V extends classes.Type> = [key: K, val: V, data: ResolveType<V>];
3622
type PropDef = <K extends string, V extends classes.Type>(key: K, val: V, data: ResolveType<V>) => PropDefinition<K, V>;
3723

0 commit comments

Comments
 (0)