Skip to content

Commit

Permalink
fix: types, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Feb 7, 2022
1 parent d8e3777 commit d26e6a5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Route Fails 1`] = `[Error: Assertion failed]`;
exports[`Route Fails 1`] = `[Error: Assertion failed: payload]`;

exports[`Route Success 1`] = `
Object {
Expand Down
5 changes: 4 additions & 1 deletion packages/input/src/Type/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TVa
public async validate(payload: unknown): Promise<TValue> {
this.assert(isObject(payload), `Wrong payload: ${this.name} expected`, payload);

const errorFields: string[] = [];
const entries: [string, any][] = [];
const validations = new Map<string, IReadableTypeError>();
for (const [field, type] of Object.entries(this.#fields)) {
Expand All @@ -49,6 +50,8 @@ export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TVa

entries.push([field, await validationType.validate(payload[field])]);
} catch (error) {
errorFields.push(field);

if (isInstanceOf(error, AssertionTypeError)) {
validations.set(field, error.toSafeJSON());
continue;
Expand All @@ -64,7 +67,7 @@ export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TVa

if (validations.size > 0) {
throw new AssertionObjectError(
"Assertion failed",
`Assertion failed: ${errorFields.join(", ")}`,
this,
payload,
entriesReverse([...validations.entries()]),
Expand Down
4 changes: 2 additions & 2 deletions packages/input/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Fields, List, Union} from "./Type";
import {Fields, List, Union, RecordType} from "./Type";
import {TypeAbstract} from "./TypeAbstract";

export type FieldFn<T> = () => T;
Expand All @@ -10,7 +10,7 @@ export type FieldsSchema<T> = {
: T[K] extends Date
? FieldType<TypeAbstract<T[K]>>
: T[K] extends Record<any, any>
? FieldType<Fields<T[K]> | Union<T[K]>>
? FieldType<Fields<T[K]> | Union<T[K]> | typeof RecordType>
: FieldType<TypeAbstract<T[K]>>;
};

Expand Down
11 changes: 8 additions & 3 deletions packages/input/test/src/Main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ describe("Test Input", () => {
});

test("RecordType", async () => {
const plainObject = {foo: 1, bar: 2};
await expect(validate(RecordType, plainObject))
const dataset = {id: Math.random(), data: {foo: 1, bar: 2}};
const schema = new Fields<{id: number; data: Record<string, any>}>({
id: Float,
data: RecordType,
});

await expect(validate(schema, dataset))
.resolves
.toEqual(plainObject);
.toEqual(dataset);

await expect(validate(RecordType, null))
.rejects
Expand Down
8 changes: 4 additions & 4 deletions packages/input/test/src/__snapshots__/Main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object {
},
},
"index": 1,
"message": "Assertion failed",
"message": "Assertion failed: children, links",
"payload": Object {
"age": 12,
"children": 1,
Expand All @@ -41,7 +41,7 @@ Object {
},
},
"index": 2,
"message": "Assertion failed",
"message": "Assertion failed: name, links",
"payload": Object {
"age": 3,
},
Expand Down Expand Up @@ -81,7 +81,7 @@ Object {
"type": "Int",
},
},
"message": "Assertion failed",
"message": "Assertion failed: age",
"payload": Object {
"links": Array [
"a",
Expand All @@ -91,7 +91,7 @@ Object {
"type": "Human",
},
},
"message": "Assertion failed",
"message": "Assertion failed: name, children, parent",
"payload": Object {
"age": 32,
"children": Array [
Expand Down

0 comments on commit d26e6a5

Please sign in to comment.