Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createValidateParse in generation mode fails to compile with Next.js #1429

Open
asterikx opened this issue Dec 13, 2024 · 4 comments
Open

createValidateParse in generation mode fails to compile with Next.js #1429

asterikx opened this issue Dec 13, 2024 · 4 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested

Comments

@asterikx
Copy link

📝 Summary

I'm using [email protected] in generation mode with [email protected]. Whenever I use createValidateParse the generated code fails to compile with next build:

Error:   × Expected '=>', got ':'
    ╭─[/Users/asterikx/app/packages/app/app/types/generated/person.ts:42:1]
 39 │         success: true,
 40 │         data: input
 41 │     } as any;
 42 │ }; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
    ·                          ─
    ╰────

Caused by:
    Syntax Error

Import trace for requested module:
./app/types/generated/person.ts

💻 Code occuring the bug

Template code:

import typia from 'typia';

type Person = {
  name: string;
  age: number;
};

export const validateParsePerson = typia.json.createValidateParse<Person>();

Generated code:

import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";
import typia from 'typia';
type Person = {
    name: string;
    age: number;
};
export const validateParsePerson = (() => { const _io0 = (input: any): boolean => "string" === typeof input.name && "number" === typeof input.age; const _vo0 = (input: any, _path: string, _exceptionable: boolean = true): boolean => ["string" === typeof input.name || _report(_exceptionable, {
        path: _path + ".name",
        expected: "string",
        value: input.name
    }), "number" === typeof input.age || _report(_exceptionable, {
        path: _path + ".age",
        expected: "number",
        value: input.age
    })].every((flag: boolean) => flag); const __is = (input: any): input is Person => "object" === typeof input && null !== input && _io0(input); let errors: any; let _report: any; const __validate = (input: any): import("typia").IValidation<Person> => {
    if (false === __is(input)) {
        errors = [];
        _report = (__typia_transform__validateReport._validateReport as any)(errors);
        ((input: any, _path: string, _exceptionable: boolean = true) => ("object" === typeof input && null !== input || _report(true, {
            path: _path + "",
            expected: "Person",
            value: input
        })) && _vo0(input, _path + "", true) || _report(true, {
            path: _path + "",
            expected: "Person",
            value: input
        }))(input, "$input", true);
        const success = 0 === errors.length;
        return success ? {
            success,
            data: input
        } : {
            success,
            errors,
            data: input
        } as any;
    }
    return {
        success: true,
        data: input
    } as any;
}; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
@samchon
Copy link
Owner

samchon commented Dec 13, 2024

I beautified the transformed code, and checked it through tsc command.

And could not find any type error. What is the problem? Anyone knows about this issue?

import typia from "typia";
import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";

type Person = {
  name: string;
  age: number;
};
export const validateParsePerson = (() => {
  const _io0 = (input: any): boolean =>
    "string" === typeof input.name &&
    "number" === typeof input.age &&
    Number.isFinite(input.age);
  const _vo0 = (
    input: any,
    _path: string,
    _exceptionable: boolean = true,
  ): boolean =>
    [
      "string" === typeof input.name ||
        _report(_exceptionable, {
          path: _path + ".name",
          expected: "string",
          value: input.name,
        }),
      ("number" === typeof input.age && Number.isFinite(input.age)) ||
        _report(_exceptionable, {
          path: _path + ".age",
          expected: "number",
          value: input.age,
        }),
    ].every((flag: boolean) => flag);
  const __is = (input: any): input is Person =>
    "object" === typeof input && null !== input && _io0(input);
  let errors: any;
  let _report: any;
  const __validate = (input: any): import("typia").IValidation<Person> => {
    if (false === __is(input)) {
      errors = [];
      _report = (__typia_transform__validateReport._validateReport as any)(
        errors,
      );
      ((input: any, _path: string, _exceptionable: boolean = true) =>
        ((("object" === typeof input && null !== input) ||
          _report(true, {
            path: _path + "",
            expected: "Person",
            value: input,
          })) &&
          _vo0(input, _path + "", true)) ||
        _report(true, {
          path: _path + "",
          expected: "Person",
          value: input,
        }))(input, "$input", true);
      const success = 0 === errors.length;
      return success
        ? {
            success,
            data: input,
          }
        : ({
            success,
            errors,
            data: input,
          } as any);
    }
    return {
      success: true,
      data: input,
    } as any;
  };
  return (
    input: string,
  ): import("typia").IValidation<import("typia").Primitive<Person>> =>
    __validate(JSON.parse(input)) as any;
})();

@samchon samchon self-assigned this Dec 13, 2024
@samchon samchon added invalid This doesn't seem right question Further information is requested labels Dec 13, 2024
samchon added a commit that referenced this issue Dec 13, 2024
@asterikx
Copy link
Author

@samchon Thank you. I can confirm that tsc works without errors, but the next build fails.

@samchon
Copy link
Owner

samchon commented Dec 13, 2024

Can you contribute a PR with test-nextjs directory? I need to test it.

@samchon samchon added help wanted Extra attention is needed good first issue Good for newcomers labels Dec 13, 2024
@asterikx
Copy link
Author

asterikx commented Dec 13, 2024

@samchon It's an issue related to SWC. Next.js uses SWC to compile the project (TSC is only used for type-checking). When running bunx swc ./app/types/generated/person.ts I get the same error:

  × Expected '=>', got ':'
    ╭─[app/types/generated/person.ts:42:1]
 39 │         success: true,
 40 │         data: input
 41 │     } as any;
 42 │ }; return (input: string): import("typia").IValidation<import("typia").Primitive<Person>> => __validate(JSON.parse(input)) as any; })();
    ·                          ─
    ╰────


Caused by:
    Syntax Error
Error: Failed to compile 1 file with swc.
    at Object.assertCompilationResult (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/util.js:149:15)
    at files (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/file.js:201:19)
    at async _default (/private/tmp/bunx-501-swc@latest/node_modules/@swc/cli/lib/swc/file.js:220:9)

It used to work with [email protected] though (probably the return type import("typia").IValidation<import("typia").Primitive<Person>> changed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants