Skip to content

Commit 4ced9e3

Browse files
committed
Added validation decorators in one file, version 0.5.0
1 parent b371579 commit 4ced9e3

18 files changed

+53
-79
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/ngx-forms-builder.svg)](https://bundlephobia.com/result?p=ngx-forms-builder) [![npm](https://img.shields.io/npm/l/express.svg?maxAge=2592000)](/LICENSE)
66

7-
A small library that adds validation with decorators and build angular forms 📝
7+
A small library that adds validation with decorators and build angular forms 🅰📝
88

99
## Demo
1010

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-forms-builder-demo",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",

projects/ngx-forms-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-forms-builder",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "A small library that adds validation with decorators and build angular forms.",
55
"keywords": [
66
"angular",

projects/ngx-forms-builder/src/lib/services/model-form-builder.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import 'reflect-metadata';
22
import { Injectable } from '@angular/core';
33
import { FormGroup, FormControl, Validators, ValidatorFn } from '@angular/forms';
4+
import { AbstractControlOptions } from '@angular/forms/src/model';
45

56
@Injectable()
67
export class ModelFormBuilder<T> {
78

8-
public build(target: T): FormGroup {
9-
const fg = new FormGroup({});
9+
public build(target: T, options?: AbstractControlOptions | null): FormGroup {
10+
const fg = new FormGroup({}, options);
1011

1112
for (const propertyKey of Object.keys(target)) {
1213

projects/ngx-forms-builder/src/lib/validations/customValidator.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

projects/ngx-forms-builder/src/lib/validations/email.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/ngx-forms-builder/src/lib/validations/exclude.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
export { Required } from './required';
2-
export { Email } from './email';
3-
export { Min } from './min';
4-
export { Max } from './max';
5-
export { Pattern } from './pattern';
6-
export { CustomValidator } from './customValidator';
7-
export { Exclude } from './exclude';
1+
export * from './validation.decorator';

projects/ngx-forms-builder/src/lib/validations/max.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/ngx-forms-builder/src/lib/validations/min.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/ngx-forms-builder/src/lib/validations/pattern.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/ngx-forms-builder/src/lib/validations/required.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Validations } from './validations.enum';
2+
import { ValidatorFn } from '@angular/forms';
3+
4+
declare type PropertyDecorator = (target: object, propertyKey: string | symbol) => void;
5+
6+
export const createValidationDecorator = (validation: Validations) => {
7+
return (): PropertyDecorator => {
8+
return (target: object, propertyKey: string) => {
9+
Reflect.defineMetadata(String(validation), true, target, propertyKey);
10+
};
11+
};
12+
};
13+
14+
export const createValidationDecoratorWithValue = <T>(validation: Validations) => {
15+
return (value?: T): PropertyDecorator => {
16+
return (target: object, propertyKey: string) => {
17+
Reflect.defineMetadata(String(validation), value, target, propertyKey);
18+
};
19+
};
20+
};
21+
22+
export const Required = createValidationDecorator(Validations.REQUIRED);
23+
export const Email = createValidationDecorator(Validations.EMAIL);
24+
export const Exclude = createValidationDecorator(Validations.EXCLUDE);
25+
export const Min = createValidationDecoratorWithValue<number>(Validations.MIN);
26+
export const Max = createValidationDecoratorWithValue<number>(Validations.MAX);
27+
export const Pattern = createValidationDecoratorWithValue<RegExp | string>(Validations.PATTERN);
28+
export const CustomValidator = createValidationDecoratorWithValue<ValidatorFn>(Validations.CUSTOMVALIDATOR);
29+
export const JP = createValidationDecoratorWithValue<RegExp | string>(Validations.PATTERN);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export enum Validations {
2+
REQUIRED = 'required',
3+
EMAIL = 'email',
4+
MIN = 'min',
5+
MAX = 'max',
6+
PATTERN = 'pattern',
7+
CUSTOMVALIDATOR = 'customValidator',
8+
EXCLUDE = 'exclude'
9+
}

src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ <h1>Ngx Forms Builder</h1>
4444
</mat-error>
4545
</mat-form-field>
4646

47+
<button mat-raised-button color="default" (click)="onReset()">Reset</button>&nbsp;
4748
<button mat-raised-button color="primary" [disabled]="formGroup.invalid">Submit</button>
4849

4950
</form>

src/app/app.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ export class AppComponent implements OnInit {
2424
duration: 2000,
2525
});
2626
}
27+
28+
onReset() {
29+
this.formGroup.reset();
30+
}
2731
}

src/app/person.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Required, Email, Pattern, Min, Max, Exclude, CustomValidator } from 'ngx-forms-builder';
2-
import { identificationValidator } from './identificationValidators';
2+
import { identificationValidator } from './validators/identification.validator';
33

44
export class Person {
55

@@ -35,4 +35,4 @@ export class Person {
3535
this.secretPassword = secretPassword;
3636
}
3737

38-
}
38+
}

src/app/identificationValidators.ts renamed to src/app/validators/identification.validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export const identificationValidator = (): ValidatorFn => {
1111
identificationValidator: false
1212
};
1313
};
14-
};
14+
};

0 commit comments

Comments
 (0)