import { InsightError } from "../controller/IInsightFacade"; import QueryValidator from "./QueryValidator"; import { ValidatorInfo } from "./QueryProcessor"; export default class QueryValidatorOptions { constructor() {} private static directions = ["UP", "DOWN"]; public static validateQueryOptions(options: any, validateInfo: ValidatorInfo): boolean { if ("COLUMNS" in options && Object.keys(options).length === 1) { this.validateColumns(options.COLUMNS, validateInfo); return true; } if ("COLUMNS" in options && "ORDER" in options && Object.keys(options).length === 2) { this.validateColumns(options.COLUMNS, validateInfo); this.validateOrder(options.ORDER, validateInfo); return true; } throw new InsightError("Wrong options!"); } public static validateColumns(columns: any[], validateInfo: ValidatorInfo): boolean { if (Array.isArray(columns) && columns.length > 0) { columns.forEach((column) => { const key: string = QueryValidator.validateAnykey(column, validateInfo); if (!key.includes("_") && !validateInfo.applyOrGroup.includes(key)) { throw new InsightError("Invalid key in columns!"); } if (validateInfo.applyOrGroup.length > 0 && !validateInfo.applyOrGroup.includes(key)) { throw new InsightError("Transformations is present yet key in columns is not in group or apply!"); } validateInfo.column.push(key); }); return true; } throw new InsightError("Wrong Columns!"); } public static validateOrder(order: any, validateInfo: ValidatorInfo): boolean { if (order === null) { throw new InsightError("Order is null!"); } if (typeof order === "object" && Object.keys(order).length === 2 && "dir" in order && "keys" in order) { if (!this.directions.includes(order.dir)) { throw new InsightError("Invalid dir!"); } if (Array.isArray(order.keys) && order.keys.length > 0) { order.keys.forEach((key: any) => { if (typeof key !== "string") { throw new InsightError("anykey is not a string!"); } QueryValidator.validateAnykey(key, validateInfo); if (!validateInfo.column.includes(key)) { throw new InsightError("Order is not in Columns!"); } }); return true; } } else if (typeof order === "string") { QueryValidator.validateAnykey(order, validateInfo); if (validateInfo.column.includes(order)) { return true; } } throw new InsightError("Order is not in Columns!"); } }