valgene_cli 1.0.11

  • Readme
  • Changelog
  • Example
  • Installing
  • 49

Valgene CLI #

Build Status Coverage Status Pub

Introduction #

assuming you are providing some RESTful/Web APIs, then you are familiar with the tasks of

  • documenting the API
  • validating incoming data against the API specification
  • creating DTOs for dedicated API endpoints

Somehow these things are disconnected to each other, that means the documentation of the API is normally not used for validating incoming data. Neither it is used for writing a DTO class. As well all the tasks are repetitive, manual and error prone.

This is where Valgene kicks in and reduces a lot of pain.

Usage #

Valgene (Validation Generator) generates validator and DTO boiler plate code from your OpenAPI specs.

Given #

so lets assume you have an API spec like the following that defines 1 endpoint that accepts incoming data, that is [POST] /pets.

      description: Creates a new pet in the store.  Duplicates are allowed
      operationId: addPet
        description: Pet to add to the store
        required: true
              $ref: '#/components/schemas/NewPet'

The payload of the endpoint is expected to be like:

    - name  
      type: string
      type: string    

When #

when invoking valgene

valgene --template php5.5 --spec petstore-expanded.yaml --option 'php.namespace:\My\PetStore\Api'

Then #

it will generate a Validator, DTO and some Exception classes:

valgene --template php5.5 --spec petstore-expanded.yaml --option 'php.namespace:\My\PetStore\Api'
> processing petstore-expanded.yaml:
  - route [POST]    /pets
> generating:
  - PostAddPet/NewPetDto.php
  - PostAddPet/NewPetDtoValidator.php
  - Exception/MissingFieldException.php
  - Exception/FieldException.php
  - Exception/InvalidFieldException.php

Generated Validator looks like this:


namespace \My\PetStore\Api\PostAddPet;

use \My\PetStore\Api\Exception\InvalidFieldException;
use \My\PetStore\Api\Exception\MissingFieldException;

class NewPetDtoValidator
     * @param array $json
     * @throws MissingFieldException
     * @throws InvalidFieldException
    public function validate($json)
        $this->isNameValid($json, true);
        $this->isTagValid($json, false);

     * @param array $json
     * @param bool $isRequired
    protected function isNameValid($json, $isRequired)
        $field = NewPetDto::PROPERTY_NAME;
        if (!array_key_exists($field, $json)) {
            if ($isRequired) {
                throw new MissingFieldException($field, $json);
        $value = $json[$field];

        if (!is_string($value)) {
            throw new InvalidFieldException($field, $json, 'datatype is not string');

     * @param array $json
     * @param bool $isRequired
    protected function isTagValid($json, $isRequired)
        $field = NewPetDto::PROPERTY_TAG;
        if (!array_key_exists($field, $json)) {
            if ($isRequired) {
                throw new MissingFieldException($field, $json);
        $value = $json[$field];

        if (!is_string($value)) {
            throw new InvalidFieldException($field, $json, 'datatype is not string');

Generated DTO looks like this:


namespace \My\Sample\Api\PostPets;

class NewPetDto
    const PROPERTY_NAME = 'name';
    const PROPERTY_TAG = 'tag';

    /** @var string $name */
    public $name;

    /** @var string $tag */
    public $tag;

     * @param array $payload
     * @return NewPetDto
    public static function fromArray(array $payload)
        $self = new static();
        $self->name = $payload[static::PROPERTY_NAME];
        $self->tag = $payload[static::PROPERTY_TAG];

        return $self;

Customization of Templates #

Be aware that the finally generated code is totally customizable and the shown example is very opinionated. To enable your custom Templates you copy the files from the template folder to a folder on you local disk. Then you can customize them and pass the path of the folder as an argument like:

valgene --template-folder $PWD/my-custom-php-templates --spec petstore-expanded.yaml --option 'php.namespace:\My\PetStore\Api'

Further reading of variables available in templates you can find here

Installation #

the dart way:

pub global activate valgene_cli

Generating code for other languages #

as seen above there is a --template parameter that allows to switch the generated language/template.

valgene --template php5.5 --spec petstore-expanded.yaml --option 'php.namespace:\My\PetStore\Api'

In fact the code generators itself are just a couple of templates that getting rendered by the valgene engine. The template language itself is Mustache and therefore you can customize the code that is generated pretty easy.

Things to be done #

  • add support for API specs in other formats such as JSON
  • add support for a config file that lives in a own project to save the command line args
  • providing templates for other languages like Java
  • working on a IDE integration for VS Code and IntelliJ to automate things even further
  • full fledged package that can be run standalone (especially in the IDE for integration)

Changelog #

1.0.11 #

  • fixed #7 strings with default value null came out as "null"

1.0.10 #

  • small improvement on DTO Validator template

1.0.9 #

  • more robust validation of nullable values

1.0.8 #

  • migrate to pedantic package for linting
  • adding cli parameter version
  • adding cli parameter help
  • add exception handling on spec file loading, to show potential yaml parsing errors

1.0.7 #

  • minor changes that pana suggested
  • mockito upgrade

1.0.6 #

  • minor naming improvement for validation methods

1.0.5 #

  • refactor the php5.5 template folder so that exceptions are generated just once
  • refactor the naming conventions for endpoints to the template by having folders called {{endpoint}} that will be substituted during generation of endpoints

1.0.4 #

  • add support for nullable nested types in the openAPI spec, so that arrays of subtypes can be null

1.0.3 #

  • implement openAPI nullable on objects that plays well with default

1.0.2 #

  • implement template-folder cli argument to give customized templates into the engine

1.0.1 #

  • implement openAPI default and allOf

1.0.0 #

  • minimal working version, with bit more examples

0.0.1 #

  • minimal working version, far away from feature complete


Example 1 #

generate validator and DTO for the PetStore petstore-expanded.yaml

step 1 download the OpenAPI specification file: #


step 2 run valgene and let the code generate: #

valgene --template php5.5 --spec petstore-expanded.yaml --option 'php.namespace:\My\PetStore\Api'

Example 2 Payload with array of Objects #

generate validator and DTO for the PetStore container-types.yaml that shows how an array of Objects is used.

step 1 download the OpenAPI specification file: #


step 2 run valgene and let the code generate: #

valgene --template php5.5 --spec container-types.yaml --option 'php.namespace:\My\PetStore\Api'

Use this package as an executable

1. Install it

You can install the package from the command line:

$ pub global activate valgene_cli

2. Use it

The package has the following executables:

$ valgene

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  valgene_cli: ^1.0.11

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:valgene_cli/valgene_cli.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Nov 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21


Detected platforms: other

Primary library: package:valgene_cli/valgene_cli.dart with components: io, mirrors.

Health issues and suggestions

Document public APIs. (-0.66 points)

172 out of 174 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/valgene_cli.dart. (-1 points)

Analysis of lib/valgene_cli.dart reported 2 hints:

line 20 col 31: Unnecessary new keyword.

line 79 col 26: Unnecessary new keyword.

Fix lib/generator.dart. (-0.50 points)

Analysis of lib/generator.dart reported 1 hint:

line 194 col 18: Unnecessary new keyword.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
args ^1.5.0 1.5.2
logging ^0.11.3+2 0.11.3+2
meta ^1.1.6 1.1.8
mustache ^1.0.2 1.1.1
recase ^2.0.0+1 2.0.1
resource ^2.1.5 2.1.6
yaml ^2.1.15 2.2.0
Transitive dependencies
charcode 1.1.2
collection 1.14.12
path 1.6.4
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
mockito ^4.0.0
pedantic ^1.0.0
test ^1.4.0
test_coverage ^0.2.0