OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.8k stars 6.58k forks source link

[REQ] Validation for typescript-angular #7405

Open AndreKoepke opened 4 years ago

AndreKoepke commented 4 years ago

This ticket is reference to keep my work tracked

Is your feature request related to a problem? Please describe.

Nope.

Subject changed

New description

typescript-angular can generate code with decorators for validation.

Old description

I would like to have a Code-Generator for validation-checks. I know that's not part of the OpenAPI spec yet. For my bachelor thesis I will try to implement that. See my fork for updates: https://github.com/AndreKoepke/openapi-generator

I will focus on java and typescript generators.

As example (from swagger pet-store example). For this spec:

  Pet:
    # ...
    properties:
      id:
        type: "integer"
        format: "int64"
        # this is a new part
        # id should be positive 
        validation:
          greaterOrEqualThan: 0
      category:
        $ref: "#/definitions/Category"
      name:
        type: "string"
        example: "doggie"
        # this is a new part
        validation:
          matchRegex: "^[a-zA-Z0-9\ \-]+$"
      photoUrls:
        type: "array"
        xml:
          name: "photoUrl"
          wrapped: true
        items:
          type: "string"
          # this is a new part
          # check for correct urls
          validation:
            matchRegex: "^http(s?)://[a-zA-Z0-9\-\.\/\?]$"
      # ...

I want to have a java-class like this

class Pet {
  private long id;
  private String name;
  private List<String> photoUrls;

  // getter and setters here

  // all validation rules should be added here
  // maybe it's better to return all failed validation-checks instead of boolean for all rules
  public boolean isValid() {
    if (!(id >= 0)) return false;
    if (!(name != null && name.matches("^[a-zA-Z0-9\ \-]+$")) return false;
    if (!(photoUrls != null && photoUrls.stream.allMatch(s -> s.matches("^http(s?)://[a-zA-Z0-9\-\.\/\?]$"))) return false;

    return true;
  }
}

Or typescript class like this

export class Pet {
  private id: number;
  private name: string;
  private photoUrls: string[];

  // getter and setters here

  public isValid() : boolean {
    if (!(this.id >= 0)) return false;
    if (!(this.name != null && new RegExp('^[a-zA-Z0-9\ \-]+$').test(this.name)) return false;
    if (!(this.photoUrls != null && this.photoUrls.every(s =>  new RegExp('^http(s?)://[a-zA-Z0-9\-\.\/\?]$').test(s))) return false;

    return true;
  }
}
AndreKoepke commented 3 years ago

I found out that a validation is already in open-api-spec.
Here is nice article how to use them: https://www.baeldung.com/spring-rest-openapi-documentation#beanvalidation The spring-generator using this validation-informations already and makes great code.

But the typescript-angular-generator cannot do validations.
So the new subject of this issue is: Change the generator to add validation-annotations in typescript.

I want to use this npm-package for annotations (aka decorators): https://github.com/typestack/class-validator
If there are other opinions, then let me know.