Skip to content
Snippets Groups Projects
ShoppingcartValidiyChecker.ts 1.71 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { ZodType } from "zod";
    
    import { Request } from "express";
    import { ValidityChecker } from "../ValidityCheker";
    import { queryParamsSchema } from "../utils/queryParamsSchema";
    
    Iheb Boussida's avatar
    Iheb Boussida committed
    import { addShoppingCartSchema, deleteShoppingCartSchema, queryNameSchema, queryShoppingartParamsSchema, updateShoppingCartSchema } from "./shoppingcart.schema";
    
    
    export class ShoppingCartValidityChecker extends ValidityChecker {
      path: string;
      req: Request;
    
      constructor(path: string, req: Request) {
        super();
        this.path = path;
        this.req = req;
      }
    
      validateUsingUrl(): boolean {
        if (this.path === "/createShoppingCart") {
          this.setSchema(addShoppingCartSchema);
          this.setObject(this.req.body);
          return this.validate();
        }
    
        if (this.path === "/getShoppingListContainingItemByName") {
          this.setSchema(queryNameSchema);
          this.setObject(this.req.query);
          return this.validate();
        }
    
        if (this.path === "/getShoppingCartByCriteria") {
          let body = { ...this.req.query };
    
    Iheb Boussida's avatar
    Iheb Boussida committed
          this.setSchema(queryShoppingartParamsSchema);
    
          this.setObject(body);
    
          const isValid = this.validate();
          if (isValid) {
            let criteria = Object.keys(body)[0];
            this.req.params.selector = criteria;
            this.req.params.value = String(body[criteria]);
          }
          return isValid;
        }
    
        if (this.path === "/updateShoppingCart") {
          this.setSchema(updateShoppingCartSchema);
          this.setObject(this.req.body);
          return this.validate();
        }
    
        if (this.path === "/deleteShoppingCart") {
          let body = { id: Number(this.req.query.id) };
          this.setSchema(deleteShoppingCartSchema);
          this.setObject(body);
          return this.validate();
        }
    
        return true;
      }
    }