Skip to content

Commit 2b71d36

Browse files
author
Sergey Netyaga
committed
updated readme and package.json
1 parent 3f5d0fc commit 2b71d36

File tree

4 files changed

+216
-6
lines changed

4 files changed

+216
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
6. [Contributing](#contribution)
1111

1212
## Supports
13-
From Angular 5.
13+
Tested only Angular 6, but it should work on lower versions as well.
1414

1515
## Install
1616
Install with npm

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "bean-validation",
3-
"version": "0.0.0",
2+
"name": "ngx-bean-validation",
3+
"version": "0.0.5",
44
"scripts": {
55
"ng": "ng",
6-
"start": "ng serve",
6+
"start": "ng serve --port 8888",
77
"build": "ng build",
88
"test": "ng test",
99
"lint": "ng lint",
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# NgxBeanValidation
2+
3+
> Idea was taken from [Bean Validation](https://beanvalidation.org/)
4+
5+
1. [Supports](#supports)
6+
2. [Install](#install)
7+
3. [What is the problem?](#what-is-the-problem)
8+
4. [Contains](#contains)
9+
5. [Does not support](#does-not-support)
10+
6. [Contributing](#contribution)
11+
12+
## Supports
13+
From Angular 5.
14+
15+
## Install
16+
Install with npm
17+
18+
`npm install ngx-bean-validation --save-dev`
19+
20+
## What is the problem?
21+
22+
Reactive forms are very powerful, but they become painful for big forms:
23+
```typescript
24+
class Component {
25+
private userForm: FormGroup = this.formBuilder.group({
26+
email: ['', Validators.compose([Validators.required, Validators.email])],
27+
name: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(40)])],
28+
age: ['', Validators.compose([Validators.number, Validators.min(18), Validators.max(60)])],
29+
creditCards: [{
30+
cardNumber: ['', Validators.compose(Validators.isCreditCard, Validators.isMasterCard)],
31+
date: ['', Validators.compose([Validators.required, Validators.pattern(/^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/)])],
32+
cvv: ['', Validators.compose([Validators.required, Validators.pattern(/^[0-9]{3,4}$/)])]
33+
}],
34+
address: {
35+
addressLine1: ['', Validators.required],
36+
addressLine2: '',
37+
city: ['', Validators.required],
38+
region: ['', Validators.required],
39+
zip: ['', Validators.compose([Validators.required, Validators.pattern(/^\d{5}(?:[-\s]\d{4})?$/)])],
40+
country: ['', Validators.required]
41+
},
42+
deliveryDate: ['', Validators.compose([Validators.required, Validators.isDate, Validators.dateBefore(someValue), Validators.dateAfter(someValue)])]
43+
});
44+
}
45+
```
46+
47+
instead of this huge unreadable peace of code, I recommend using Bean Validation approach
48+
```typescript
49+
class User {
50+
@Email()
51+
@Required()
52+
email: string;
53+
54+
@MaxLength(40)
55+
@MinLength(3)
56+
@Required()
57+
name: string;
58+
59+
@Max(60)
60+
@Min(18)
61+
@Number()
62+
@Required()
63+
age: number;
64+
65+
@NestedArray()
66+
creditCards: CreditCard[] = [new CreditCard()];
67+
68+
@Nested()
69+
address: Address = new Address();
70+
71+
@DateAfter(new Date())
72+
@DateBefore(new Date())
73+
@IsDate()
74+
@Required()
75+
deliveryDate: string;
76+
}
77+
78+
class CreditCard {
79+
@IsMasterCard()
80+
@IsCreditCard()
81+
@Required()
82+
cardNumber: string;
83+
84+
@Pattern(/^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/)
85+
@Required()
86+
date: string;
87+
88+
@Pattern(/^[0-9]{3,4}$/)
89+
@Required()
90+
cvv: string;
91+
}
92+
93+
class Address {
94+
@Required()
95+
addressLine1: string;
96+
97+
@EmptyControl()
98+
@Required()
99+
addressLine2: string;
100+
101+
@Required()
102+
city: string;
103+
104+
@Required()
105+
region: string;
106+
107+
@Pattern(/^\d{5}(?:[-\s]\d{4})?$/)
108+
@Required()
109+
zip: string;
110+
111+
@Required()
112+
country: string;
113+
}
114+
115+
class Component {
116+
private userForm: FormGroup = new BeanFormGroup<User>(new User());
117+
}
118+
```
119+
120+
Now we can use our classes as interface and reuse them for reactive forms.
121+
122+
## Contains:
123+
124+
This library provides `BeanFormGroup` class for creation `FormGroup` from annotated classes.
125+
126+
### Annotations for default angular validators:
127+
* Email
128+
* Max
129+
* MaxLength
130+
* Min
131+
* MinLength
132+
* Required
133+
* RequiredTrue
134+
* Pattern
135+
136+
### Annotation for empty form control:
137+
* EmptyControl
138+
139+
### Annotation for nested FormGroup and FormArray:
140+
* Nested
141+
* NestedArray
142+
143+
### Annotation for disabling form control:
144+
* Disabled
145+
146+
### Function that helps you create your own validator annotation:
147+
* setSyncValidator
148+
149+
## How to use:
150+
Example how to create your own validator annotation:
151+
```typescript
152+
import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms';
153+
import {setSyncValidator, AnnotationFunction} from 'ngx-bean-validation';
154+
155+
const customAngularValidator = (someValue: any): ValidatorFn => {
156+
return (control: AbstractControl): ValidationErrors => {
157+
return {
158+
custom: 'Custom validator'
159+
};
160+
};
161+
};
162+
163+
export const CustomValidator = (someValue: any): AnnotationFunction => (target: object, key: string): void => {
164+
setSyncValidator(target, key, customAngularValidator(someValue));
165+
};
166+
```
167+
168+
Now you can put it in your class:
169+
```typescript
170+
import {CustomValidator} from './custom-validator';
171+
172+
class User {
173+
@CustomValidator('someValue')
174+
name: string
175+
}
176+
```
177+
178+
And create form group:
179+
```typescript
180+
import {FromGroup} from '@angular/forms';
181+
import {User} from './user';
182+
import {BeanFormGroup} from 'ngx-bean-validation';
183+
184+
class Component {
185+
userForm: FromGroup = new BeanFormGroup(new User())
186+
}
187+
```
188+
189+
## Does not support
190+
* Async validators
191+
* FormControl and FormArray them self, only inside FormGroup
192+
193+
## Contribution
194+
Are very welcome! Please share your ideas and improvements.
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
{
22
"name": "ngx-bean-validation",
3-
"version": "0.0.1",
3+
"version": "0.0.5",
4+
"author": {
5+
"name": "jurr90",
6+
"email": "jurr90@gmail.com"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/mimacom/ngx-bean-validation"
11+
},
12+
"keywords": [
13+
"angular",
14+
"angular6",
15+
"reactive-forms",
16+
"forms",
17+
"bean-validation",
18+
"validation"
19+
],
420
"peerDependencies": {
521
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",
622
"@angular/core": "^6.0.0-rc.0 || ^6.0.0"
723
}
8-
}
24+
}

0 commit comments

Comments
 (0)