GithubHelp home page GithubHelp logo

ozbob / ngx-form-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from verizonconnect/ngx-form-generator

0.0 0.0 0.0 1.74 MB

Generate Angular ReactiveForms from OpenAPI documents

License: MIT License

JavaScript 10.49% TypeScript 89.51%

ngx-form-generator's Introduction

Angular Form Generator

Generates an Angular ReactiveForm from a Swagger or OpenAPI definition.

Validation rules should have a single source of truth. These rules should be exposed to consumers to apply them. By doing this we can be sure that the same rules for UI validation are enforced at the API layer.

Commitizen friendly

Install

# install locally in project
npm install @verizonconnect/ngx-form-generator --save-dev

# install globally
npm install @verizonconnect/ngx-form-generator -g

CLI Usage

ngx-form-generator -f swagger.json -o projects/forms/src/lib/

# when installed locally in project run with npx
npx ngx-form-generator -f swagger.yaml -o projects/forms/src/lib/
Option Alias Comment Required
--version Show version number
--input-spec -i, --swaggerUrl Location of the OpenAPI spec as URL or file path โœ“
--output -o, --outDir Where to write the generated files
--file-name -f, --outFile Generated file name
--help -h Show help

Example CLI Usage

ngx-form-generator -i https://petstore.swagger.io/v2/swagger.json -o petstore-forms
ngx-form-generator -i https://petstore.swagger.io/v2/swagger.yaml -o petstore-forms
npx ngx-form-generator -i swagger.json -o project/form/src/lib

Library Usage

import { loadSpec, makeForm, saveFile } from '@verizonconnect/ngx-form-generator';

async function main() {
  const spec = await loadSpec('swagger.json');
  const form = makeForm(spec);
  await saveFile(form, 'projects/forms/src/lib/index.ts');
}
await main();

Examples Generated Form

import { FormGroup, FormControl, Validators } from '@angular/forms';

export const addressModelForm = new FormGroup({
  firstName: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[a-zA-Z\'\s]+$/),
    Validators.minLength(1),
    Validators.maxLength(100)
  ]),
  lastName: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[a-zA-Z\'\s]+$/),
    Validators.minLength(1),
    Validators.maxLength(100)
  ]),
  address: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[\w\'\s]+$/),
    Validators.minLength(1),
    Validators.maxLength(100)
  ]),
  address2: new FormControl(null, [
    Validators.pattern(/^[\w\'\s]+$/),
    Validators.minLength(1),
    Validators.maxLength(100)
  ]),
  city: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[\w\'\s]+$/),
    Validators.minLength(1),
    Validators.maxLength(100)
  ]),
  postalCode: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[\w\s]+$/),
    Validators.minLength(4),
    Validators.maxLength(8)
  ]),
  emailAddress: new FormControl(null, [
    Validators.required,
    Validators.pattern(/^[\w\@\!\#\%\&\'\*\+\-\/\=\?\`\{\|\}\~\.]+$/),
    Validators.email
  ])
});

License

MIT

ngx-form-generator's People

Contributors

martinmcwhorter avatar dependabot[bot] avatar nilshoffmann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.