deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

@deepkit/type cannot validate mapped type #281

Closed freddi301 closed 2 years ago

freddi301 commented 2 years ago

error

- Expected  - 1
+ Received  + 7

- Array []
+ Array [
+   ValidationErrorItem {
+     "code": "type",
+     "message": "Invalid type",
+     "path": "",
+   },
+ ]

test

import { validate } from "@deepkit/type";

test("deepkit features", () => {
  type Api = {
    inc(x: number): number;
    dup(x: string): string;
  };
  type Request = {
    [Method in keyof Api]: {
      method: Method;
      arguments: Parameters<Api[Method]>;
    };
  }[keyof Api];
  type Response = {
    [Method in keyof Api]: {
      method: Method;
      result: ReturnType<Api[Method]>;
    };
  }[keyof Api];
  expect(
    validate<Request>({ method: "inc", arguments: [4] } as Request)
  ).toEqual([]);
  expect(
    validate<Request>({ method: "dup", arguments: [""] } as Request)
  ).toEqual([]);
  expect(validate<Request>({ method: "inc", arguments: [""] })).not.toEqual([]);
  expect(validate<Request>({ method: "unc", arguments: [""] })).not.toEqual([]);
  expect(validate<Response>({ method: "inc", result: 4 } as Response)).toEqual(
    []
  );
  expect(validate<Request>({ method: "dup", result: "" } as Response)).toEqual(
    []
  );
  expect(validate<Response>({ method: "inc", result: "" })).not.toEqual([]);
  expect(validate<Request>({ method: "enc", result: "" })).not.toEqual([]);
});
marcj commented 2 years ago

Fixed in https://github.com/deepkit/deepkit-framework/commit/13ef6877ff4874f66ad82e5a81476fe6aa43c4a5. Thanks for reporting!

freddi301 commented 2 years ago

Amazing work! Thank you How it can be used before release? Feel free to close the issue.

marcj commented 2 years ago

How it can be used before release?

That's rather tricky. There is an explanation in our Discord, but that works only locally.