Skip to content

Commit 3f5d0fc

Browse files
author
Sergey Netyaga
committed
updated readme, updated public api
1 parent beddf84 commit 3f5d0fc

File tree

7 files changed

+74
-30
lines changed

7 files changed

+74
-30
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
> Idea was taken from [Bean Validation](https://beanvalidation.org/)
44
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+
520
## What is the problem?
621

722
Reactive forms are very powerful, but they become painful for big forms:
@@ -109,6 +124,7 @@ Now we can use our classes as interface and reuse them for reactive forms.
109124
This library provides `BeanFormGroup` class for creation `FormGroup` from annotated classes.
110125

111126
### Annotations for default angular validators:
127+
* Email
112128
* Max
113129
* MaxLength
114130
* Min
@@ -133,13 +149,26 @@ This library provides `BeanFormGroup` class for creation `FormGroup` from annota
133149
## How to use:
134150
Example how to create your own validator annotation:
135151
```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+
136163
export const CustomValidator = (someValue: any): AnnotationFunction => (target: object, key: string): void => {
137164
setSyncValidator(target, key, customAngularValidator(someValue));
138165
};
139166
```
140167

141168
Now you can put it in your class:
142169
```typescript
170+
import {CustomValidator} from './custom-validator';
171+
143172
class User {
144173
@CustomValidator('someValue')
145174
name: string
@@ -148,12 +177,16 @@ class User {
148177

149178
And create form group:
150179
```typescript
180+
import {FromGroup} from '@angular/forms';
181+
import {User} from './user';
182+
import {BeanFormGroup} from 'ngx-bean-validation';
183+
151184
class Component {
152185
userForm: FromGroup = new BeanFormGroup(new User())
153186
}
154187
```
155188

156-
## Does`t support
189+
## Does not support
157190
* Async validators
158191
* FormControl and FormArray them self, only inside FormGroup
159192

projects/ngx-bean-validation/src/lib/annotations/empty-control.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
import {AnnotationFunction, checkAnnotationConflicts, FormMetadata, setName} from './common';
33

44
/**
5-
* If you don`t need to validate field, just use this annotation, otherwise you will not get formControl in ControlConfig
6-
* @returns {AnnotationFunction}
7-
* @constructor
5+
* If you don`t need to validate field, just use this annotation, otherwise you will not get formControl in the ControlConfig
86
*/
97
export const EmptyControl = (): AnnotationFunction => (target: any, key: string) => {
108
let metadata = Reflect.get(target, setName(key)) as FormMetadata;

projects/ngx-bean-validation/src/lib/annotations/index.ts

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

projects/ngx-bean-validation/src/public_api.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,17 @@
33
*/
44

55
export * from './lib/bean-form-group';
6-
export * from './lib/annotations';
6+
export * from './lib/annotations/disabled';
7+
export * from './lib/annotations/empty-control';
8+
export * from './lib/annotations/max';
9+
export * from './lib/annotations/email';
10+
export * from './lib/annotations/max-length';
11+
export * from './lib/annotations/min';
12+
export * from './lib/annotations/min-length';
13+
export * from './lib/annotations/nested';
14+
export * from './lib/annotations/nested-array';
15+
export * from './lib/annotations/required';
16+
export * from './lib/annotations/required-true';
17+
export * from './lib/annotations/pattern';
18+
export {setSyncValidator, AnnotationFunction} from './lib/annotations/common';
19+

src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
[class.is-invalid]="password.invalid"
2626
formControlName="password"
2727
placeholder="Password">
28+
{{password.errors | json}}
2829
</div>
2930
<div class="form-group form-check">
3031
<input type="checkbox"

src/app/app.component.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import {Component} from '@angular/core';
2-
import {FormGroup} from '@angular/forms';
3-
import {Email} from '../../projects/ngx-bean-validation/src/lib/annotations/email';
4-
import {MaxLength, MinLength, Required, RequiredTrue} from '../../projects/ngx-bean-validation/src/lib/annotations';
5-
import {Pattern} from '../../projects/ngx-bean-validation/src/lib/annotations/pattern';
6-
import {BeanFormGroup} from '../../projects/ngx-bean-validation/src/lib/bean-form-group';
2+
import {AbstractControl, FormGroup, ValidationErrors, ValidatorFn, Validators} from '@angular/forms';
3+
import {BeanFormGroup, Pattern, Email, MaxLength, RequiredTrue, MinLength, setSyncValidator, AnnotationFunction} from 'ngx-bean-validation';
4+
5+
const customRequired = (): ValidatorFn => {
6+
return (control: AbstractControl): ValidationErrors => {
7+
if (control.valid) {
8+
const validationError = Validators.required(control);
9+
10+
if (validationError) {
11+
return {
12+
required: 'Custom required'
13+
};
14+
}
15+
}
16+
17+
return null;
18+
};
19+
};
20+
21+
export const CustomRequired = (): AnnotationFunction => (target: object, key: string): void => {
22+
setSyncValidator(target, key, customRequired());
23+
};
724

825
class User {
926
@Email()
@@ -12,7 +29,7 @@ class User {
1229
@Pattern(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/)
1330
@MaxLength(24)
1431
@MinLength(4)
15-
@Required()
32+
@CustomRequired()
1633
password: string;
1734

1835
@RequiredTrue()

tsconfig.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
"dom"
1919
],
2020
"paths": {
21-
"ng-bean-validation": [
22-
"dist/ng-bean-validation"
23-
],
24-
"ng-bean-validation/*": [
25-
"dist/ng-bean-validation/*"
26-
],
2721
"ngx-bean-validation": [
2822
"dist/ngx-bean-validation"
2923
],
@@ -32,4 +26,4 @@
3226
]
3327
}
3428
}
35-
}
29+
}

0 commit comments

Comments
 (0)