private addParamToSchema()

in lib/liveValidation/liveValidatorLoader.ts [169:246]


  private addParamToSchema(schema: Schema, params: Parameter[] | undefined, operation: Operation) {
    if (params === undefined) {
      return;
    }

    const properties = schema.properties!;
    for (const p of params) {
      const param = this.jsonLoader.resolveRefObj(p);
      switch (param.in) {
        case "body":
          // ignore validation in case of file type
          if (param.schema?.type === "file") {
            break;
          }
          properties.body = param.schema ?? {};
          if (param.required) {
            copyInfo(param, schema);
            this.addRequiredToSchema(schema, "body");
          } else {
            operation._bodyTransform = bodyTransformIfNotRequiredAndEmpty;
          }
          break;

        case "header":
          const name = param.name.toLowerCase();
          if (param.required) {
            this.addRequiredToSchema(properties.headers, name);
          }
          param.required = undefined;
          properties.headers.properties![name] = param as Schema;
          addParamTransform(operation, param);
          break;

        case "query":
          if (shouldSkipQueryParam(param)) {
            break;
          }
          if (param.required) {
            this.addRequiredToSchema(properties.query, param.name);
          }
          // Remove param.required as it have different meaning in swagger and json schema
          param.required = undefined;
          properties.query.properties![param.name] = param as Schema;
          addParamTransform(operation, param);
          break;

        case "path":
          if (shouldSkipPathParam(param)) {
            break;
          }
          // if (!param.required) {
          //   throw new Error("Path property mush be required");
          // }
          this.addRequiredToSchema(properties.path, param.name);
          param.required = undefined;
          properties.path.properties![param.name] = param as Schema;
          addParamTransform(operation, param);
          break;

        case "formData":
          // ignore validation in case of file type
          if (param.type === "file") {
            break;
          }
          if (param.required) {
            this.addRequiredToSchema(properties.formData, param.name);
          }
          // Remove param.required as it have different meaning in swagger and json schema
          param.required = undefined;
          properties.formData.properties![param.name] = param as Schema;
          addParamTransform(operation, param);
          break;

        default:
          throw new Error(`Not Supported parameter in: ${param}`);
      }
    }
  }