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

JSON parser that never throws #1053

Closed
Stadly opened this issue May 8, 2024 · 4 comments
Closed

JSON parser that never throws #1053

Stadly opened this issue May 8, 2024 · 4 comments
Assignees
Labels
duplicate This issue or pull request already exists question Further information is requested

Comments

@Stadly
Copy link

Stadly commented May 8, 2024

When using json.isParse when data on the client, there is no guarantee that the data is valid JSON, since the user may have tampered with it. In such cases, json.isParse throws SyntaxError. This makes typia cumbersome to use, since I have to handle exceptions when all I want to know is whether the data parses to the correct type. When the data is not parsable it does not parse to the correct type, so I would expect the function to just return null in such cases.

This would be a breaking change, so I suggest either adding a parameter to isParse or creating a separate safeParse that returns null when an error is thrown.

For example:

export const safeParse = <Type>(input: string) => {
  try {
    return assertParse<Type>(input);
  } catch (error) {
    return null;
  }
};

I tried just making this helper function, but then I get a warning: [vite] warning: @rollup/plugin-typescript TS(typia.json.assertParse): non-specified generic argument. Seems like I can't use typia with generics?

@samchon
Copy link
Owner

samchon commented May 8, 2024

You have to specify the generic argument.

Recommend to utilize the callback function in that case.

@samchon samchon self-assigned this May 8, 2024
@samchon samchon added the duplicate This issue or pull request already exists label May 8, 2024
@Stadly
Copy link
Author

Stadly commented May 14, 2024

Thanks for your reply, @samchon.

It makes sense that the generic argument must be specified.

What do you mean by the callback function? I can't find anything about callbacks in the documentation.

Also, which issue is this a duplicate of, since you added the duplicate label? I wasn't able to find any.

@samchon samchon added the question Further information is requested label May 14, 2024
@samchon
Copy link
Owner

samchon commented May 14, 2024

import typia, { tags } from "typia";

interface IMember {
  id: string & tags.Format<"uuid">;
  name: string;
  age: number &
    tags.Type<"uint32"> &
    tags.Minimum<20> &
    tags.ExclusiveMaximum<100>;
}

const safeParse = 
  <T>(parser: (input: string) => T) => 
  (str: string): T | null => {
    try {
      return parser(str);
    } catch {
      return null;
    }
  };

console.log(
  safeParse(
    typia.json.createAssertParse<IMember>(),
  )(
    typia.json.stringify(
      typia.random<IMember>(),
    ),
  ),
);

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDANHA3g1BzAZzgF84AzKCEOAIiRVRoG4AoF4AOxgFMozUAxtzgBJALLcQAI17YWcOMAAmALjgEYUTnjgAyXIQB0AMWghUMADw0ArjeU0AfKwUdUIbmo1aOeF3HxPOA4baVldeQUDAkMAFWRua3suAGYAJic9SIUYfBixTlBQyzSABkcsqOjDAFEADwEAGxsCYAA3bjFUOqKQSwBGUvLWYjYBCA4NdVQybgAFVCgCYQBeOEjLWMcACjBF5ag1bc4wGxgvTW0ASjgVitibu-WFbe8LnzwrtVi4AB9gmyNRq3CpYbIIKCIORVBRQbgwGxQDhwPZLXivTRXfwKUgCCwCAAW0JhcIRSIBQOxJEixFYLHGkwgjW4hkaEDw20iBBm8323E5VXoaEMACsCBNDAI4RZuABBAgHGALNGWcSSGRQHZXdCRK4CqJC1Ci8UcQzebTAMiIfWC5DCqCoDhKKiqiRhTXbbXgr0KL1YoA

Here it is.

@Stadly
Copy link
Author

Stadly commented May 15, 2024

Thanks!

@samchon samchon closed this as completed May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants