OpenAPITools / OpenAPITools/openapi-generator

[REQ] Validation for typescript-angular

Open
#7,405 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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;
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-angular generator and its model-generation entry point; the issue does not name specific files or tests. Review the current generator behavior and the linked fork's status, then define how validation rules should be represented in the OpenAPI input and generated TypeScript classes before implementation. Done means the requested validation behavior is generated and covered by relevant generator tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.