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

Separate input validation from database query #33

Closed
G0maa opened this issue Jan 1, 2023 · 3 comments
Closed

Separate input validation from database query #33

G0maa opened this issue Jan 1, 2023 · 3 comments
Assignees
Labels
refactor Things that need to be refactored.

Comments

@G0maa
Copy link
Owner

G0maa commented Jan 1, 2023

  • As suggested by @Amr2812, and @b-fuze.
  • Results in: Better Separation of Concerns & more Clean Code.
  • Reference:
    • export const ToLikeQuery = (attribute: string | undefined) => {
      if (!attribute) return;
      return { [Op.like]: `%${attribute}%` };
      };
    • export const ZStudentQuery = ZStudent.partial()
      .extend({
      classId: ZStudent.shape.classId.transform((attribute) =>
      ToLikeQuery(attribute)
      ), // duplicate use Z
      parentName: ZStudent.shape.parentName.transform((attribute) =>
      ToLikeQuery(attribute)
      ),
      parentPhonenumber: ZStudent.shape.parentPhonenumber.transform((attribute) =>
      ToLikeQuery(attribute)
      ),
      })
      .partial();
@G0maa G0maa added the refactor Things that need to be refactored. label Jan 1, 2023
@G0maa G0maa self-assigned this Jan 1, 2023
@G0maa

This comment was marked as outdated.

G0maa added a commit that referenced this issue Mar 26, 2023
@G0maa
Copy link
Owner Author

G0maa commented Mar 26, 2023

  • API Paging #50 Solves this ever lasting issue, but as advised by @Amr2812, my solution is a hacky one and it will eventually bite me (again), a better solution would be hardcoding the parameters to be querified.

G0maa added a commit that referenced this issue Apr 7, 2023
* Pagination of all routes
* Finally separating querification from validation
- Solves #33 & #50 
- More simple tests for helpers.ts
@G0maa
Copy link
Owner Author

G0maa commented Apr 7, 2023

Solution as of now:

// This is a hakcky solution as told by @Amr2812,
// and eventually it's gonna bite me, the "better" way
// is to make a "mapping" function (i.e. from field to querified field),
// for each schema.
// P.S: This function sometimes gets input that does not match the schema,
// i.e. mainly missing page & size.
const querifyStringFields = (
object: Record<string, unknown>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: z.ZodObject<any, any>
) => {
// console.log('schema.shape', schema.shape);
for (const key in schema.shape) {
// for ... in loops over everything,
// this if condition is to make sure it only loops over the attributes only.
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, no-prototype-builtins
if (!schema.shape.hasOwnProperty(key)) continue;
// Just making sure key exists in the object, might be optional.
if (object[key] === undefined) continue;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const field = schema.shape[key];
// For ZodOptional<ZodString> and similar cases.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
const typeName = mostInnerType(field);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (typeName === 'ZodString')
object[key] = { [Op.like]: `%${object[key]}%` };
}
return object;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Things that need to be refactored.
Projects
None yet
Development

No branches or pull requests

1 participant