-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Suggestion - assertParseQueryString #815
Comments
This is too domestic issue. Instead, I can suggest you alternative solutions
|
What do you mean? Do you mean it's not for Typia? Anyways thanks for suggestions. We don't use Fastify nor NestJS. |
Then can you suggest a function interface better than I think it would better to make it into a new namespace. Also, restrictions would be like below: |
If doing it in a proper way, by analogy with json parse functions in Typia, I'd suggest export namespace queryString {
export function isParse<T>(input: string): Primitive<T> | null;
export function assertParse<T>(input: string): Primitive<T>;
export function validateParse<T>(
input: string
): IValidation<Primitive<T>>;
} we could inherit interface Type {
a: number[];
b: 'foo' | 'bar';
}
typia.queryString.assertParse<Type>('?a=1&b=foo&a=3'); // Fine, { a: [1, 3], b: "foo" }
typia.queryString.assertParse<Type>('?a=1&b=foo'); // Fine, { a: [1], b: "foo" }
typia.queryString.assertParse<Type>('?a=1&b=foo&a=uh-oh'); // Invalid
typia.queryString.assertParse<Type>('?a=1&b=buzz'); // Invalid |
No encoding function required? |
Do you mean object -> string encoding? For our (API) needs, no. On the client, our encoding function has quite trivial implementation. From the PoV of Typia library, for the sake of the completeness it's fine to use this api. |
Upgrade to |
You're 🔥! Thank you! Seems to work flawlessly. However I've noticed this in the comment to
That text in bold actually is scary. What's the purpose of P.S. We also can't migrate to v5 as customValidators seem to be dropped. Documenting any alternatives would be much appreciated 🙌 |
Then you If you read a little bit more about the comment, you may find the way. |
Does it literally mean to do this? interface MyQuery {
a: number;
b: string;
}
const myQuery = http.query<MyQuery>(req.originalUrl);
assertQuery(myQuery);
// ... If so, I can't think of a use case to be honest (in the HTTP api at least) where |
Like this: import typia, { tags } from "typia";
interface IMember {
id: string & tags.Format<"uuid">;
email: string & tags.Format<"email">;
age: number
& tags.Type<"uint32">
& tags.Minimum<20>
& tags.ExclusiveMaximum<100>;
}
typia.http.createAssertQuery<IMember>(); |
Thanks! Could you please clarify the end-application usage of this 😇 Am I correct it would be the following: import typia, { tags } from "typia";
interface IMember {
id: string & tags.Format<"uuid">;
email: string & tags.Format<"email">;
age: number
& tags.Type<"uint32">
& tags.Minimum<20>
& tags.ExclusiveMaximum<100>;
}
const assertQuery = typia.http.createAssertQuery<IMember>();
// Custom function that wraps, for instance, Express js framework or whatever
defineMyApiEndpoint(async (req, res) => {
const myQuery = http.query<IMember>(req.originalUrl);
assertQuery(myQuery);
// work with myQuery
}); |
Oh my god, what the hell are you doing? Just choose one of below: import typia, { tags } from "typia";
interface IMember {
id: string & tags.Format<"uuid">;
email: string & tags.Format<"email">;
age: number
& tags.Type<"uint32">
& tags.Minimum<20>
& tags.ExclusiveMaximum<100>;
}
const assertQuery = typia.http.createAssertQuery<IMember>();
// Custom function that wraps, for instance, Express js framework or whatever
defineMyApiEndpoint(async (req, res) => {
const myQuery = assertQuery(req.originalUrl);
// work with myQuery
}); import typia, { tags } from "typia";
interface IMember {
id: string & tags.Format<"uuid">;
email: string & tags.Format<"email">;
age: number
& tags.Type<"uint32">
& tags.Minimum<20>
& tags.ExclusiveMaximum<100>;
}
// Custom function that wraps, for instance, Express js framework or whatever
defineMyApiEndpoint(async (req, res) => {
const myQuery = typia.http.assertQuery<IMember>(req.originalUrl);
// work with myQuery
}); |
😅 Thank you a ton, this explains everything. So far we actually used it like this: import { assertEquals } from 'typia';
apiAuthenticatedEntry<ApiRequestBody, ApiResponseBody>(async (req, res) => {
assertEquals(req.body); // req.params is typed to ApiRequestBody and typia infers this type for a check
// work with req.body - they're already parsed and typed by the underlying framework,
// and we use typia just for the runtime check
}); |
Hello!
We're using your awesome library for input validation with (pretty much) express.js framework. Great job, neat solution! I would like to ask about implementing assertions for query strings.
In express (as well as everywhere else), when passing a query string
?foo=1&bar=2
,req.query
gets this object:...which is validated by typia in our case with
assertEquals(req.query)
.I am now wondering whether we can do a step further and instead of defining
Make it be able to parse and validate against this interface:
To ultimately get this output with no validation errors:
Feel free to close this request if it feels boring or keep it to collect feedback :) But this feature would allow to pass pretty much any (primitive) type in a query string and properly decode it. We have an automatically generated client library in our project which infers and reuses all types defined in the api, and having to always define strings in query even in case of a number feels like it can be fixed.
At the same time, I understand this can be very complex to implement.
Thanks!
The text was updated successfully, but these errors were encountered: