valgene_cli 0.0.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 42

Valgene CLI #

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.

paths:
  /pets:
    post:
      description: Creates a new pet in the store.  Duplicates are allowed
      operationId: addPet
      requestBody:
        description: Pet to add to the store
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewPet'

The payload of the endpoint is expected to be like:

NewPet:
  required:
    - name  
  properties:
    name:
      type: string
    tag:
      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:
  - PostPets/MissingFieldException.php
  - PostPets/NewPetDto.php
  - PostPets/NewPetDtoValidator.php
  - PostPets/FieldException.php
  - PostPets/InvalidFieldException.php

Generated Validator looks like this:

<?php

namespace \My\Sample\Api\PostPets;

/**
 * GENERATED CODE - DO NOT MODIFY BY HAND
 */
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 (!isset($json[$field]) && $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 (!isset($json[$field]) && $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:

<?php

namespace \My\Sample\Api\PostPets;

/**
 * GENERATED CODE - DO NOT MODIFY BY HAND
 */
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 #

Be aware that the finally generated code is totally customizable and the shown example is very opinionated. You will find below further infos how to write your own templates.

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.

// TODO explain how in detail the a custom template can be used / provided

Things to come #

  • add more examples and test cases for more complex schemas
  • 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)

0.0.1 #

  • minimal working version, far away from feature complete

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:


dependencies:
  valgene_cli: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter 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';
  
Version Uploaded Documentation Archive
1.0.11 May 20, 2019 Go to the documentation of valgene_cli 1.0.11 Download valgene_cli 1.0.11 archive
1.0.10 May 4, 2019 Go to the documentation of valgene_cli 1.0.10 Download valgene_cli 1.0.10 archive
1.0.9 May 3, 2019 Go to the documentation of valgene_cli 1.0.9 Download valgene_cli 1.0.9 archive
1.0.8 May 3, 2019 Go to the documentation of valgene_cli 1.0.8 Download valgene_cli 1.0.8 archive
1.0.7 Apr 30, 2019 Go to the documentation of valgene_cli 1.0.7 Download valgene_cli 1.0.7 archive
1.0.6 Apr 29, 2019 Go to the documentation of valgene_cli 1.0.6 Download valgene_cli 1.0.6 archive
1.0.5 Feb 8, 2019 Go to the documentation of valgene_cli 1.0.5 Download valgene_cli 1.0.5 archive
1.0.4 Feb 6, 2019 Go to the documentation of valgene_cli 1.0.4 Download valgene_cli 1.0.4 archive
1.0.3 Nov 26, 2018 Go to the documentation of valgene_cli 1.0.3 Download valgene_cli 1.0.3 archive
1.0.2 Nov 25, 2018 Go to the documentation of valgene_cli 1.0.2 Download valgene_cli 1.0.2 archive

All 13 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
42
Learn more about scoring.

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

  • Dart: 2.3.0
  • pana: 0.12.15

Platforms

Detected platforms: Flutter, other

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

Health issues and suggestions

Document public APIs. (-0.77 points)

127 out of 128 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/types.dart. (-4.89 points)

Analysis of lib/types.dart reported 10 hints, including:

line 27 col 7: Don't explicitly initialize variables to null.

line 27 col 20: Use = to separate a named parameter from its default value.

line 28 col 7: Don't explicitly initialize variables to null.

line 28 col 20: Use = to separate a named parameter from its default value.

line 29 col 7: Don't explicitly initialize variables to null.

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

Analysis of lib/generator.dart reported 1 hint:

line 107 col 1: Prefer using /// for doc comments.

Format lib/parser.dart.

Run dartfmt to format lib/parser.dart.

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://github.com/valgene.dart/valgene.dart-cli was unreachable.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and valgene_cli.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
args ^1.5.0 1.5.1
logging ^0.11.3+2 0.11.3+2
meta ^1.1.6 1.1.7
mustache ^1.0.2 1.1.1
recase ^2.0.0+1 2.0.1
yaml ^2.1.15 2.1.15
Transitive dependencies
charcode 1.1.2
collection 1.14.11
path 1.6.2
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
Dev dependencies
mockito ^3.0.0
test ^1.4.0