[go: up one dir, main page]

double-agent-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.70 • Public • Published

DoubleAgentValidator

This package serves as complement to the java library called doubleagentvalidator.

This is a javascript library which allow to reuse the same json schemas used in the backend rest application to validates the data at the frontend.

This libs depends on:

How to use:

1. Install the package in your application:

Recommended: Use yarn to install this package. This package manager will create yarn.lock file, to avoid broken packages caused by npm inconsistent versions.

# Install yarn package manager 
npm i -g yarn
 
# Use yarn to install double-agent 
yarn install double-agent-validator

If you get the error PhantomJS not found on PATH, just do that:

TMP_DIR=/tmp yarn install

Or use npm to install this package

npm i --save double-agent-validator

2. In your AppModule initialization inject a factory to get the validation script from the backend and fill-in the DoubleAgentValdiator service with the schemas, keywords and formats.

 
 
import {  NgModule } from '@angular/core';
import { DoubleAgentValidatorModule, DOUBLE_AGENT_VALIDATOR_SCHEMA_URL} from 'double-agent-validator';
 
 
@NgModule(
  {
    imports: [ DoubleAgentValidatorModule ],
    providers: [
      {
        provide: 'http://localhost:8080/schemas', /* here you should point to your double-validator backend endpoint */
        useValue: url
      }
    ]
  }
)
export class AppModule { }

Great!!! You just did it, now the following services are available on your Angular 2 App:

DoubleAgentValidatorService

This class allow you to validate some data in a service layer using its validate method, which checks some data againts an specific schema you pass to the service.

Your service will be something like the example bellow:

 
import { DoubleAgentValidator } from 'double-agent-validator';
import { ValidationResult } from 'double-agent-validator/models';
 
@Injectable()
class MyService {
  constructor(private doubleAgentValidator: DoubleAgentValidator) {
 
  }
 
 
  validateContribuinte(): ValidationResult {
    /***
    // returns something like this
    {
      hasErrors: true;
      errors: [
        keyword: 'required',
        dataPath: '.id',
        message: 'field is missing'
      ];
    }
    */
    return this.doubleAgentValidator.validate('contribuinte-v1', data);
  }
}
DoubleAgentFormGroupBuilder

This class can be used to build FormGroup containing the controls representing each property in the schema including the respective validators.

Example:

import { DoubleAgentFormGroupBuilder } from 'double-agent-validator';
import { FormGroup } from '@angular/forms';
 
@Component({
  selector: 'my-form',
  template: `
  <form [formGroup]="formGroup">
    <div class="form-group">
      <label for="nome">Nome: </label>
      <input type="text" id="nome" formControlName="nome"> 
    </div>
    <!-- your another controls for the others properties goes here too -->
  </form>`
})
export class MyFormComponent {
    private shemaName = 'contribuinte-v1';
    private formGroup: FormGroup;
    constructor(private doubleAgentFormGroupBuilder: DoubleAgentFormGroupBuilder) {
      this.formGroup = doubleAgentFormGroupBuilder.build(this.schemaName);
    }
}

If you want create formGroup without validators because your application will handle the validation process itself, you can do this:

import { DoubleAgentFormGroupBuilder } from 'double-agent-validator';
import { FormGroup } from '@angular/forms';
 
@Component({
  selector: 'my-form',
  template: `
  <form [formGroup]="formGroup">
    <div class="form-group">
      <label for="nome">Nome: </label>
      <input type="text" id="nome" formControlName="nome"> 
    </div>
    <!-- your another controls for the others properties goes here too -->
  </form>`
})
export class MyFormComponent {
    private shemaName = 'contribuinte-v1';
    private formGroup: FormGroup;
    constructor(private doubleAgentFormGroupBuilder: DoubleAgentFormGroupBuilder) {
      this.formGroup = doubleAgentFormGroupBuilder.build(this.schemaName, {
        createValidators: false
      });
    }
}

TODO

  • Provide the service to allow error messages mapping

  • Website to share the library api documentation

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.700latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.700
1.0.690
5.0.30
5.0.20
5.0.10
5.0.00
1.0.680
1.0.670
1.0.660
1.0.650
1.0.640
1.0.630
1.0.620
1.0.610
1.0.600
1.0.590
1.0.580
1.0.570
1.0.560
1.0.550
1.0.540
1.0.530
1.0.520
1.0.510
1.0.47-ng4patch0
1.0.500
1.0.490
1.0.480
1.0.470
1.0.460
1.0.450
1.0.440
1.0.430
1.0.420
1.0.400
1.0.390
1.0.380
1.0.370
1.0.360
1.0.350
1.0.340
1.0.330
1.0.320
1.0.310
1.0.300
1.0.290
1.0.280
1.0.270
1.0.260
1.0.250
1.0.240
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i double-agent-validator

Weekly Downloads

0

Version

1.0.70

License

MIT

Unpacked Size

434 kB

Total Files

164

Last publish

Collaborators

  • abneroliveira
  • brunobastosg
  • mfdeveloper