File tree Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { useContext , useRef } from 'react' ;
2
+
3
+ import { TranslatorContext } from '@ids-context/Translator' ;
4
+
5
+ import BaseValidator from '../../validators/BaseValidator' ;
6
+ import ValidatorManager from '../../validators/ValidatorManager' ;
7
+
8
+ export default ( validators : BaseValidator [ ] = [ ] ) => {
9
+ const translator = useContext ( TranslatorContext ) ;
10
+ const validatorManagerInstance = useRef ( new ValidatorManager ( validators , { translator } ) ) ;
11
+
12
+ return validatorManagerInstance . current ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ import { TranslatorType } from '@ids-context/Translator' ;
2
+
3
+ export default abstract class BaseValidator {
4
+ protected _translator ! : TranslatorType ;
5
+
6
+ setTranslator ( translator : TranslatorType ) {
7
+ this . _translator = translator ;
8
+ }
9
+
10
+ abstract getErrorMessage ( ) : string ;
11
+
12
+ abstract validate ( _value : unknown ) : boolean ;
13
+ }
14
+
15
+ export type BaseValidatorType = typeof BaseValidator ;
Original file line number Diff line number Diff line change
1
+ import BaseValidator from './BaseValidator' ;
2
+
3
+ export default class IsEmptyStringValidator extends BaseValidator {
4
+ getErrorMessage ( ) : string {
5
+ const Translator = this . _translator ;
6
+
7
+ return Translator . trans ( /*@Desc ("This field cannot be empty.")*/ 'ibexa.validators.is_empty_string' ) ;
8
+ }
9
+
10
+ validate ( value : string ) : boolean {
11
+ return value . trim ( ) !== '' ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ import { TranslatorType } from '@ids-context/Translator/Translator.types' ;
2
+
3
+ import BaseValidator from './BaseValidator' ;
4
+
5
+ interface ValidatorManagerOptions {
6
+ translator : TranslatorType ;
7
+ }
8
+
9
+ export default class ValidatorManager {
10
+ private _validators : BaseValidator [ ] ;
11
+ private _translator : TranslatorType ;
12
+
13
+ constructor ( validators : BaseValidator [ ] = [ ] , { translator } : ValidatorManagerOptions ) {
14
+ validators . forEach ( ( validator ) => {
15
+ validator . setTranslator ( translator ) ;
16
+ } ) ;
17
+
18
+ this . _validators = validators ;
19
+ this . _translator = translator ;
20
+ }
21
+
22
+ addValidator ( validator : BaseValidator ) : void {
23
+ validator . setTranslator ( this . _translator ) ;
24
+
25
+ this . _validators . push ( validator ) ;
26
+ }
27
+
28
+ removeValidator ( validator : BaseValidator ) : void {
29
+ this . _validators = this . _validators . filter ( ( savedValidator ) => savedValidator !== validator ) ;
30
+ }
31
+
32
+ validate ( value : unknown ) {
33
+ const errors = this . _validators
34
+ . filter ( ( validator : BaseValidator ) => ! validator . validate ( value ) )
35
+ . map ( ( validator : BaseValidator ) => validator . getErrorMessage ( ) ) ;
36
+
37
+ return { isValid : ! errors . length , messages : errors } ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments