Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @module
* @todo Move to `src/validator/`.
*/

/**
* Validation error codes.
*
Expand Down
1 change: 0 additions & 1 deletion src/random/generators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
import {cloneBinary} from '@jsonjoy.com/util/lib/json-clone';
import type {AbstractType} from '../type/classes/AbstractType';
import type {AnyType} from '../type/classes/AnyType';
import type {ArrayType} from '../type/classes/ArrayType';
import type {BinaryType} from '../type/classes/BinaryType';
Expand Down
21 changes: 0 additions & 21 deletions src/system/Method.ts

This file was deleted.

111 changes: 0 additions & 111 deletions src/system/TypeRouter.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/system/__tests__/TypeSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ describe('.toString()', () => {
└─ validators
└─ "empty-string""
`);
});

test('prints type with nested self reference', () => {
const system = new TypeSystem();
const {t} = system;
system.alias('User', t.obj.prop('id', t.str).opt('friend', t.Ref('User')));
expect(system.toString()).toMatchInlineSnapshot(`
"TypeSystem
├─ aliases
│ └─ User
│ └─ obj
│ ├─ "id":
│ │ └─ str
│ └─ "friend"?:
│ └─ ref → [User]
└─ validators"
`);
});
});
47 changes: 0 additions & 47 deletions src/system/__tests__/toTypeScript.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {TypeSystem} from '..';
import {TypeRouter} from '../TypeRouter';

test('generates TypeScript source for simple string type', () => {
const system = new TypeSystem();
Expand Down Expand Up @@ -92,49 +91,3 @@ test('type interface inside a tuple', () => {
"
`);
});

test('can export whole router', () => {
const system = new TypeSystem();
const {t} = system;
const router = new TypeRouter({system, routes: {}}).extend(() => ({
callMe: t.Function(t.str, t.num),
'block.subscribe': t.Function$(t.Object(t.prop('id', t.str)), t.obj),
}));
expect(router.toTypeScript()).toMatchInlineSnapshot(`
"export namespace Router {
export type Routes = {
callMe: (request: string) => Promise<number>;
"block.subscribe": (request$: Observable<{
id: string;
}>) => Observable<{}>;
};
}
"
`);
});

test('can export whole router and aliases', () => {
const system = new TypeSystem();
const {t} = system;
system.alias('Document', t.Object(t.prop('id', t.str), t.prop('title', t.str)).options({title: 'The document'}));
const router = new TypeRouter({system, routes: {}}).extend(() => ({
callMe: t.Function(t.str, t.num),
'block.subscribe': t.Function$(t.Object(t.prop('id', t.str)), t.Ref('Document')),
}));
expect(router.toTypeScript()).toMatchInlineSnapshot(`
"export namespace Router {
export type Routes = {
callMe: (request: string) => Promise<number>;
"block.subscribe": (request$: Observable<{
id: string;
}>) => Observable<Document>;
};

export interface Document {
id: string;
title: string;
}
}
"
`);
});
16 changes: 16 additions & 0 deletions src/type/__tests__/validateTestSuite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type Type, t} from '..';
import {ValidationError, ValidationErrorMessage} from '../../constants';
import {TypeSystem} from '../../system/TypeSystem';

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

test('object nested in array', () => {
const type = t.obj.prop('foo', t.array(t.obj.prop('bar', t.str)));
validate(type, {foo: [{bar: 'baz'}]});
const validator = type.validator('object');
const res = validator({foo: [{bar: 'baz'}]});
expect(res).toBe(null);
const res2 = validator({foo: {bar: 'baz'}});
expect(res2).toEqual({
code: ValidationError[ValidationError.ARR],
errno: ValidationError.ARR,
message: ValidationErrorMessage[ValidationError.ARR],
path: ['foo'],
});
});

describe('size bounds', () => {
test('respects min and max', () => {
const type = t.arr.options({min: 2, max: 4});
Expand Down
1 change: 0 additions & 1 deletion src/type/classes/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {BinaryEncoderCodegenContext} from '../../codegen/binary/BinaryEncod
import type {SchemaOf, Type} from '../types';
import type {TypeSystem} from '../../system/TypeSystem';
import type {json_string} from '@jsonjoy.com/util/lib/json-brand';
import type * as ts from '../../typescript/types';
import type {TypeExportContext} from '../../system/TypeExportContext';

export class ArrayType<T extends Type> extends AbstractType<schema.ArraySchema<SchemaOf<T>>> {
Expand Down
14 changes: 0 additions & 14 deletions src/value/ObjectValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ export type ObjectValueToTypeMap<F> = ToObject<{
}>;
export type TuplesToFields<T> = T extends PropDefinition<infer K, infer V>[] ? classes.ObjectFieldType<K, V>[] : never;

// export type MergeObjectsTypes<A, B> =
// A extends classes.ObjectType<infer A2>
// ? B extends classes.ObjectType<infer B2>
// ? classes.ObjectType<[...A2, ...B2]> :
// never :
// never;

// export type MergeObjectValues<A, B> =
// A extends ObjectValue<infer A2>
// ? B extends ObjectValue<infer B2>
// ? ObjectValue<MergeObjectsTypes<A2, B2>> :
// never :
// never;

type PropDefinition<K extends string, V extends classes.Type> = [key: K, val: V, data: ResolveType<V>];
type PropDef = <K extends string, V extends classes.Type>(key: K, val: V, data: ResolveType<V>) => PropDefinition<K, V>;

Expand Down