@websolutespa/bom-mixer-forms 0.3.5 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +22 -22
- package/dist/index.mjs +22 -22
- package/package.json +2 -2
- package/src/forms/validators/email.validator.ts +2 -2
- package/src/forms/validators/match.validator.ts +5 -2
- package/src/forms/validators/max-length.validator.ts +2 -2
- package/src/forms/validators/max.validator.ts +2 -2
- package/src/forms/validators/min-length.validator.ts +2 -2
- package/src/forms/validators/min.validator.ts +2 -2
- package/src/forms/validators/pattern.validator.ts +2 -2
- package/src/forms/validators/required-if.validator.ts +3 -3
- package/src/forms/validators/required-true.validator.ts +2 -2
- package/src/forms/validators/required.validator.ts +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @websolutespa/bom-mixer-forms
|
|
2
2
|
|
|
3
|
+
## 1.8.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 15ad595: Added: Validators parametric error key
|
|
8
|
+
|
|
9
|
+
## 1.8.0
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [f8a1d97]
|
|
14
|
+
- @websolutespa/bom-core@1.8.0
|
|
15
|
+
- @websolutespa/bom-mixer-hooks@1.8.0
|
|
16
|
+
|
|
3
17
|
## 0.3.5
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -261,32 +261,32 @@ declare function stringToValue(value: string | string[] | undefined, options?: I
|
|
|
261
261
|
/**
|
|
262
262
|
* an email pattern validator
|
|
263
263
|
*/
|
|
264
|
-
declare function EmailValidator(): FormValidator;
|
|
264
|
+
declare function EmailValidator(key?: string): FormValidator;
|
|
265
265
|
|
|
266
266
|
/**
|
|
267
267
|
* an equality match validation on another field
|
|
268
268
|
*/
|
|
269
|
-
declare function MatchValidator(getOtherValue: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => any): FormValidator;
|
|
269
|
+
declare function MatchValidator(getOtherValue: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => any, key?: string): FormValidator;
|
|
270
270
|
|
|
271
271
|
/**
|
|
272
272
|
* a max string length validator
|
|
273
273
|
*/
|
|
274
|
-
declare function MaxLengthValidator(maxlength: number): FormValidator;
|
|
274
|
+
declare function MaxLengthValidator(maxlength: number, key?: string): FormValidator;
|
|
275
275
|
|
|
276
276
|
/**
|
|
277
277
|
* a max number value validator
|
|
278
278
|
*/
|
|
279
|
-
declare function MaxValidator(max: number): FormValidator;
|
|
279
|
+
declare function MaxValidator(max: number, key?: string): FormValidator;
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
282
|
* a min string length validator
|
|
283
283
|
*/
|
|
284
|
-
declare function MinLengthValidator(minlength: number): FormValidator;
|
|
284
|
+
declare function MinLengthValidator(minlength: number, key?: string): FormValidator;
|
|
285
285
|
|
|
286
286
|
/**
|
|
287
287
|
* a min number value validator
|
|
288
288
|
*/
|
|
289
|
-
declare function MinValidator(min: number): FormValidator;
|
|
289
|
+
declare function MinValidator(min: number, key?: string): FormValidator;
|
|
290
290
|
|
|
291
291
|
/**
|
|
292
292
|
* a null validator
|
|
@@ -296,22 +296,22 @@ declare function NullValidator(): FormValidator;
|
|
|
296
296
|
/**
|
|
297
297
|
* a regex pattern validator
|
|
298
298
|
*/
|
|
299
|
-
declare function PatternValidator(pattern: string | RegExp): FormValidator;
|
|
299
|
+
declare function PatternValidator(pattern: string | RegExp, key?: string): FormValidator;
|
|
300
300
|
|
|
301
301
|
/**
|
|
302
302
|
* a required dependant on another field
|
|
303
303
|
*/
|
|
304
|
-
declare function RequiredIfValidator(condition: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => boolean): FormValidator;
|
|
304
|
+
declare function RequiredIfValidator(condition: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => boolean, key?: string): FormValidator;
|
|
305
305
|
|
|
306
306
|
/**
|
|
307
307
|
* a required and true validator
|
|
308
308
|
*/
|
|
309
|
-
declare function RequiredTrueValidator(): FormValidator;
|
|
309
|
+
declare function RequiredTrueValidator(key?: string): FormValidator;
|
|
310
310
|
|
|
311
311
|
/**
|
|
312
312
|
* a required validator
|
|
313
313
|
*/
|
|
314
|
-
declare function RequiredValidator(): FormValidator;
|
|
314
|
+
declare function RequiredValidator(key?: string): FormValidator;
|
|
315
315
|
|
|
316
316
|
declare function useControl<T>(control: FormAbstract): [FormState<T>, (value: T | null) => void, () => void, () => void, FormAbstract];
|
|
317
317
|
|
package/dist/index.js
CHANGED
|
@@ -864,11 +864,11 @@ function formGroup(controls = {}, validators) {
|
|
|
864
864
|
}
|
|
865
865
|
|
|
866
866
|
// src/forms/validators/email.validator.ts
|
|
867
|
-
function EmailValidator() {
|
|
867
|
+
function EmailValidator(key = "email") {
|
|
868
868
|
const regex = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
869
869
|
return function(value) {
|
|
870
870
|
if (typeof value === "string") {
|
|
871
|
-
return regex.test(value) ? null : {
|
|
871
|
+
return regex.test(value) ? null : { [key]: true };
|
|
872
872
|
} else {
|
|
873
873
|
return null;
|
|
874
874
|
}
|
|
@@ -876,23 +876,23 @@ function EmailValidator() {
|
|
|
876
876
|
}
|
|
877
877
|
|
|
878
878
|
// src/forms/validators/match.validator.ts
|
|
879
|
-
function MatchValidator(getOtherValue) {
|
|
879
|
+
function MatchValidator(getOtherValue, key = "match") {
|
|
880
880
|
return function(value, rootValue, control, root) {
|
|
881
881
|
let otherValue = getOtherValue(value, rootValue, control, root);
|
|
882
882
|
otherValue = validValue(otherValue);
|
|
883
|
-
return value !== otherValue ? {
|
|
883
|
+
return value !== otherValue ? { [key]: { value, match: otherValue } } : null;
|
|
884
884
|
};
|
|
885
885
|
}
|
|
886
886
|
|
|
887
887
|
// src/forms/validators/max-length.validator.ts
|
|
888
|
-
function MaxLengthValidator(maxlength) {
|
|
888
|
+
function MaxLengthValidator(maxlength, key = "maxlength") {
|
|
889
889
|
return function(value) {
|
|
890
890
|
if (maxlength == null) {
|
|
891
891
|
return null;
|
|
892
892
|
}
|
|
893
893
|
if (typeof value === "string") {
|
|
894
894
|
const length = value ? value.length : 0;
|
|
895
|
-
return length > maxlength ? {
|
|
895
|
+
return length > maxlength ? { [key]: { requiredLength: maxlength, actualLength: length } } : null;
|
|
896
896
|
} else {
|
|
897
897
|
return null;
|
|
898
898
|
}
|
|
@@ -900,25 +900,25 @@ function MaxLengthValidator(maxlength) {
|
|
|
900
900
|
}
|
|
901
901
|
|
|
902
902
|
// src/forms/validators/max.validator.ts
|
|
903
|
-
function MaxValidator(max) {
|
|
903
|
+
function MaxValidator(max, key = "max") {
|
|
904
904
|
return function(value) {
|
|
905
905
|
value = typeof value === "string" ? parseFloat(value) : typeof value === "number" ? value : null;
|
|
906
906
|
if (value == null || max == null) {
|
|
907
907
|
return null;
|
|
908
908
|
}
|
|
909
|
-
return !isNaN(value) && value > max ? {
|
|
909
|
+
return !isNaN(value) && value > max ? { [key]: { max, actual: value } } : null;
|
|
910
910
|
};
|
|
911
911
|
}
|
|
912
912
|
|
|
913
913
|
// src/forms/validators/min-length.validator.ts
|
|
914
|
-
function MinLengthValidator(minlength) {
|
|
914
|
+
function MinLengthValidator(minlength, key = "minlength") {
|
|
915
915
|
return function(value) {
|
|
916
916
|
if (minlength == null) {
|
|
917
917
|
return null;
|
|
918
918
|
}
|
|
919
919
|
if (typeof value === "string") {
|
|
920
920
|
const length = value ? value.length : 0;
|
|
921
|
-
return length < minlength ? {
|
|
921
|
+
return length < minlength ? { [key]: { requiredLength: minlength, actualLength: length } } : null;
|
|
922
922
|
} else {
|
|
923
923
|
return null;
|
|
924
924
|
}
|
|
@@ -926,13 +926,13 @@ function MinLengthValidator(minlength) {
|
|
|
926
926
|
}
|
|
927
927
|
|
|
928
928
|
// src/forms/validators/min.validator.ts
|
|
929
|
-
function MinValidator(min) {
|
|
929
|
+
function MinValidator(min, key = "min") {
|
|
930
930
|
return function(value) {
|
|
931
931
|
value = typeof value === "string" ? parseFloat(value) : typeof value === "number" ? value : null;
|
|
932
932
|
if (value == null || min == null) {
|
|
933
933
|
return null;
|
|
934
934
|
}
|
|
935
|
-
return !isNaN(value) && value < min ? {
|
|
935
|
+
return !isNaN(value) && value < min ? { [key]: { min, actual: value } } : null;
|
|
936
936
|
};
|
|
937
937
|
}
|
|
938
938
|
|
|
@@ -944,14 +944,14 @@ function NullValidator() {
|
|
|
944
944
|
}
|
|
945
945
|
|
|
946
946
|
// src/forms/validators/pattern.validator.ts
|
|
947
|
-
function PatternValidator(pattern) {
|
|
947
|
+
function PatternValidator(pattern, key = "pattern") {
|
|
948
948
|
return function(value) {
|
|
949
949
|
if (pattern == null) {
|
|
950
950
|
return null;
|
|
951
951
|
}
|
|
952
952
|
if (typeof value === "string") {
|
|
953
953
|
const regex = patternToRegEx(pattern);
|
|
954
|
-
return regex.test(value) ? null : {
|
|
954
|
+
return regex.test(value) ? null : { [key]: { requiredPattern: regex.toString(), actualValue: value } };
|
|
955
955
|
} else {
|
|
956
956
|
return null;
|
|
957
957
|
}
|
|
@@ -971,13 +971,13 @@ function patternToRegEx(pattern) {
|
|
|
971
971
|
|
|
972
972
|
// src/forms/validators/required-if.validator.ts
|
|
973
973
|
var import_bom_core3 = require("@websolutespa/bom-core");
|
|
974
|
-
function RequiredIfValidator(condition) {
|
|
974
|
+
function RequiredIfValidator(condition, key = "required") {
|
|
975
975
|
return function(value, rootValue, control, root) {
|
|
976
976
|
if (Boolean(condition(value, rootValue, control, root)) === true) {
|
|
977
977
|
if ((0, import_bom_core3.isArray)(value) || typeof value === "string") {
|
|
978
|
-
return value.length === 0 ? {
|
|
978
|
+
return value.length === 0 ? { [key]: true } : null;
|
|
979
979
|
} else {
|
|
980
|
-
return value == null ? {
|
|
980
|
+
return value == null ? { [key]: true } : null;
|
|
981
981
|
}
|
|
982
982
|
} else {
|
|
983
983
|
return null;
|
|
@@ -986,20 +986,20 @@ function RequiredIfValidator(condition) {
|
|
|
986
986
|
}
|
|
987
987
|
|
|
988
988
|
// src/forms/validators/required-true.validator.ts
|
|
989
|
-
function RequiredTrueValidator() {
|
|
989
|
+
function RequiredTrueValidator(key = "required") {
|
|
990
990
|
return function(value) {
|
|
991
|
-
return value === true ? null : {
|
|
991
|
+
return value === true ? null : { [key]: true };
|
|
992
992
|
};
|
|
993
993
|
}
|
|
994
994
|
|
|
995
995
|
// src/forms/validators/required.validator.ts
|
|
996
996
|
var import_bom_core4 = require("@websolutespa/bom-core");
|
|
997
|
-
function RequiredValidator() {
|
|
997
|
+
function RequiredValidator(key = "required") {
|
|
998
998
|
return function(value) {
|
|
999
999
|
if ((0, import_bom_core4.isArray)(value) || typeof value === "string") {
|
|
1000
|
-
return value.length === 0 ? {
|
|
1000
|
+
return value.length === 0 ? { [key]: true } : null;
|
|
1001
1001
|
} else {
|
|
1002
|
-
return value == null ? {
|
|
1002
|
+
return value == null ? { [key]: true } : null;
|
|
1003
1003
|
}
|
|
1004
1004
|
};
|
|
1005
1005
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -816,11 +816,11 @@ function formGroup(controls = {}, validators) {
|
|
|
816
816
|
}
|
|
817
817
|
|
|
818
818
|
// src/forms/validators/email.validator.ts
|
|
819
|
-
function EmailValidator() {
|
|
819
|
+
function EmailValidator(key = "email") {
|
|
820
820
|
const regex = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
821
821
|
return function(value) {
|
|
822
822
|
if (typeof value === "string") {
|
|
823
|
-
return regex.test(value) ? null : {
|
|
823
|
+
return regex.test(value) ? null : { [key]: true };
|
|
824
824
|
} else {
|
|
825
825
|
return null;
|
|
826
826
|
}
|
|
@@ -828,23 +828,23 @@ function EmailValidator() {
|
|
|
828
828
|
}
|
|
829
829
|
|
|
830
830
|
// src/forms/validators/match.validator.ts
|
|
831
|
-
function MatchValidator(getOtherValue) {
|
|
831
|
+
function MatchValidator(getOtherValue, key = "match") {
|
|
832
832
|
return function(value, rootValue, control, root) {
|
|
833
833
|
let otherValue = getOtherValue(value, rootValue, control, root);
|
|
834
834
|
otherValue = validValue(otherValue);
|
|
835
|
-
return value !== otherValue ? {
|
|
835
|
+
return value !== otherValue ? { [key]: { value, match: otherValue } } : null;
|
|
836
836
|
};
|
|
837
837
|
}
|
|
838
838
|
|
|
839
839
|
// src/forms/validators/max-length.validator.ts
|
|
840
|
-
function MaxLengthValidator(maxlength) {
|
|
840
|
+
function MaxLengthValidator(maxlength, key = "maxlength") {
|
|
841
841
|
return function(value) {
|
|
842
842
|
if (maxlength == null) {
|
|
843
843
|
return null;
|
|
844
844
|
}
|
|
845
845
|
if (typeof value === "string") {
|
|
846
846
|
const length = value ? value.length : 0;
|
|
847
|
-
return length > maxlength ? {
|
|
847
|
+
return length > maxlength ? { [key]: { requiredLength: maxlength, actualLength: length } } : null;
|
|
848
848
|
} else {
|
|
849
849
|
return null;
|
|
850
850
|
}
|
|
@@ -852,25 +852,25 @@ function MaxLengthValidator(maxlength) {
|
|
|
852
852
|
}
|
|
853
853
|
|
|
854
854
|
// src/forms/validators/max.validator.ts
|
|
855
|
-
function MaxValidator(max) {
|
|
855
|
+
function MaxValidator(max, key = "max") {
|
|
856
856
|
return function(value) {
|
|
857
857
|
value = typeof value === "string" ? parseFloat(value) : typeof value === "number" ? value : null;
|
|
858
858
|
if (value == null || max == null) {
|
|
859
859
|
return null;
|
|
860
860
|
}
|
|
861
|
-
return !isNaN(value) && value > max ? {
|
|
861
|
+
return !isNaN(value) && value > max ? { [key]: { max, actual: value } } : null;
|
|
862
862
|
};
|
|
863
863
|
}
|
|
864
864
|
|
|
865
865
|
// src/forms/validators/min-length.validator.ts
|
|
866
|
-
function MinLengthValidator(minlength) {
|
|
866
|
+
function MinLengthValidator(minlength, key = "minlength") {
|
|
867
867
|
return function(value) {
|
|
868
868
|
if (minlength == null) {
|
|
869
869
|
return null;
|
|
870
870
|
}
|
|
871
871
|
if (typeof value === "string") {
|
|
872
872
|
const length = value ? value.length : 0;
|
|
873
|
-
return length < minlength ? {
|
|
873
|
+
return length < minlength ? { [key]: { requiredLength: minlength, actualLength: length } } : null;
|
|
874
874
|
} else {
|
|
875
875
|
return null;
|
|
876
876
|
}
|
|
@@ -878,13 +878,13 @@ function MinLengthValidator(minlength) {
|
|
|
878
878
|
}
|
|
879
879
|
|
|
880
880
|
// src/forms/validators/min.validator.ts
|
|
881
|
-
function MinValidator(min) {
|
|
881
|
+
function MinValidator(min, key = "min") {
|
|
882
882
|
return function(value) {
|
|
883
883
|
value = typeof value === "string" ? parseFloat(value) : typeof value === "number" ? value : null;
|
|
884
884
|
if (value == null || min == null) {
|
|
885
885
|
return null;
|
|
886
886
|
}
|
|
887
|
-
return !isNaN(value) && value < min ? {
|
|
887
|
+
return !isNaN(value) && value < min ? { [key]: { min, actual: value } } : null;
|
|
888
888
|
};
|
|
889
889
|
}
|
|
890
890
|
|
|
@@ -896,14 +896,14 @@ function NullValidator() {
|
|
|
896
896
|
}
|
|
897
897
|
|
|
898
898
|
// src/forms/validators/pattern.validator.ts
|
|
899
|
-
function PatternValidator(pattern) {
|
|
899
|
+
function PatternValidator(pattern, key = "pattern") {
|
|
900
900
|
return function(value) {
|
|
901
901
|
if (pattern == null) {
|
|
902
902
|
return null;
|
|
903
903
|
}
|
|
904
904
|
if (typeof value === "string") {
|
|
905
905
|
const regex = patternToRegEx(pattern);
|
|
906
|
-
return regex.test(value) ? null : {
|
|
906
|
+
return regex.test(value) ? null : { [key]: { requiredPattern: regex.toString(), actualValue: value } };
|
|
907
907
|
} else {
|
|
908
908
|
return null;
|
|
909
909
|
}
|
|
@@ -923,13 +923,13 @@ function patternToRegEx(pattern) {
|
|
|
923
923
|
|
|
924
924
|
// src/forms/validators/required-if.validator.ts
|
|
925
925
|
import { isArray } from "@websolutespa/bom-core";
|
|
926
|
-
function RequiredIfValidator(condition) {
|
|
926
|
+
function RequiredIfValidator(condition, key = "required") {
|
|
927
927
|
return function(value, rootValue, control, root) {
|
|
928
928
|
if (Boolean(condition(value, rootValue, control, root)) === true) {
|
|
929
929
|
if (isArray(value) || typeof value === "string") {
|
|
930
|
-
return value.length === 0 ? {
|
|
930
|
+
return value.length === 0 ? { [key]: true } : null;
|
|
931
931
|
} else {
|
|
932
|
-
return value == null ? {
|
|
932
|
+
return value == null ? { [key]: true } : null;
|
|
933
933
|
}
|
|
934
934
|
} else {
|
|
935
935
|
return null;
|
|
@@ -938,20 +938,20 @@ function RequiredIfValidator(condition) {
|
|
|
938
938
|
}
|
|
939
939
|
|
|
940
940
|
// src/forms/validators/required-true.validator.ts
|
|
941
|
-
function RequiredTrueValidator() {
|
|
941
|
+
function RequiredTrueValidator(key = "required") {
|
|
942
942
|
return function(value) {
|
|
943
|
-
return value === true ? null : {
|
|
943
|
+
return value === true ? null : { [key]: true };
|
|
944
944
|
};
|
|
945
945
|
}
|
|
946
946
|
|
|
947
947
|
// src/forms/validators/required.validator.ts
|
|
948
948
|
import { isArray as isArray2 } from "@websolutespa/bom-core";
|
|
949
|
-
function RequiredValidator() {
|
|
949
|
+
function RequiredValidator(key = "required") {
|
|
950
950
|
return function(value) {
|
|
951
951
|
if (isArray2(value) || typeof value === "string") {
|
|
952
|
-
return value.length === 0 ? {
|
|
952
|
+
return value.length === 0 ? { [key]: true } : null;
|
|
953
953
|
} else {
|
|
954
|
-
return value == null ? {
|
|
954
|
+
return value == null ? { [key]: true } : null;
|
|
955
955
|
}
|
|
956
956
|
};
|
|
957
957
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@websolutespa/bom-mixer-forms",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Mixer Forms module of the BOM Repository",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bom",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"scripts": {
|
|
16
16
|
"lint": "eslint *.ts*",
|
|
17
17
|
"test": "jest",
|
|
18
|
-
"test-watch": "npm test -- --watch",
|
|
18
|
+
"test-watch": "npm test -- --watch --silent=false",
|
|
19
19
|
"coverage:integration": "npm run test -- --coverage",
|
|
20
20
|
"coverage": "run-s -c --silent coverage:*",
|
|
21
21
|
"compile": "tsup ./src/index.ts --format esm,cjs --dts --external react",
|
|
@@ -3,11 +3,11 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* an email pattern validator
|
|
5
5
|
*/
|
|
6
|
-
export function EmailValidator(): FormValidator {
|
|
6
|
+
export function EmailValidator(key: string = 'email'): FormValidator {
|
|
7
7
|
const regex = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
8
8
|
return function (value) {
|
|
9
9
|
if (typeof value === 'string') {
|
|
10
|
-
return regex.test(value) ? null : {
|
|
10
|
+
return regex.test(value) ? null : { [key]: true };
|
|
11
11
|
} else {
|
|
12
12
|
return null;
|
|
13
13
|
}
|
|
@@ -5,10 +5,13 @@ import { validValue } from '../utils';
|
|
|
5
5
|
/**
|
|
6
6
|
* an equality match validation on another field
|
|
7
7
|
*/
|
|
8
|
-
export function MatchValidator(
|
|
8
|
+
export function MatchValidator(
|
|
9
|
+
getOtherValue: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => any,
|
|
10
|
+
key: string = 'match'
|
|
11
|
+
): FormValidator {
|
|
9
12
|
return function (value, rootValue, control, root) {
|
|
10
13
|
let otherValue = getOtherValue(value, rootValue, control, root);
|
|
11
14
|
otherValue = validValue(otherValue);
|
|
12
|
-
return value !== otherValue ? {
|
|
15
|
+
return value !== otherValue ? { [key]: { value: value, match: otherValue } } : null;
|
|
13
16
|
};
|
|
14
17
|
}
|
|
@@ -3,14 +3,14 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a max string length validator
|
|
5
5
|
*/
|
|
6
|
-
export function MaxLengthValidator(maxlength: number): FormValidator {
|
|
6
|
+
export function MaxLengthValidator(maxlength: number, key: string = 'maxlength'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
if (maxlength == null) {
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
11
|
if (typeof value === 'string') {
|
|
12
12
|
const length = value ? value.length : 0;
|
|
13
|
-
return length > maxlength ? {
|
|
13
|
+
return length > maxlength ? { [key]: { requiredLength: maxlength, actualLength: length } } : null;
|
|
14
14
|
} else {
|
|
15
15
|
return null;
|
|
16
16
|
}
|
|
@@ -3,12 +3,12 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a max number value validator
|
|
5
5
|
*/
|
|
6
|
-
export function MaxValidator(max: number): FormValidator {
|
|
6
|
+
export function MaxValidator(max: number, key: string = 'max'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
value = typeof value === 'string' ? parseFloat(value) : typeof value === 'number' ? value : null;
|
|
9
9
|
if (value == null || max == null) { // loose
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
12
|
-
return !isNaN(value) && value > max ? {
|
|
12
|
+
return !isNaN(value) && value > max ? { [key]: { max: max, actual: value } } : null;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -3,14 +3,14 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a min string length validator
|
|
5
5
|
*/
|
|
6
|
-
export function MinLengthValidator(minlength: number): FormValidator {
|
|
6
|
+
export function MinLengthValidator(minlength: number, key: string = 'minlength'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
if (minlength == null) {
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
11
|
if (typeof value === 'string') {
|
|
12
12
|
const length = value ? value.length : 0;
|
|
13
|
-
return length < minlength ? {
|
|
13
|
+
return length < minlength ? { [key]: { requiredLength: minlength, actualLength: length } } : null;
|
|
14
14
|
} else {
|
|
15
15
|
return null;
|
|
16
16
|
}
|
|
@@ -3,12 +3,12 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a min number value validator
|
|
5
5
|
*/
|
|
6
|
-
export function MinValidator(min: number): FormValidator {
|
|
6
|
+
export function MinValidator(min: number, key: string = 'min'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
value = typeof value === 'string' ? parseFloat(value) : typeof value === 'number' ? value : null;
|
|
9
9
|
if (value == null || min == null) { // loose
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
12
|
-
return !isNaN(value) && value < min ? {
|
|
12
|
+
return !isNaN(value) && value < min ? { [key]: { min: min, actual: value } } : null;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -3,14 +3,14 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a regex pattern validator
|
|
5
5
|
*/
|
|
6
|
-
export function PatternValidator(pattern: string | RegExp): FormValidator {
|
|
6
|
+
export function PatternValidator(pattern: string | RegExp, key: string = 'pattern'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
if (pattern == null) { // loose
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
11
|
if (typeof value === 'string') {
|
|
12
12
|
const regex = patternToRegEx(pattern);
|
|
13
|
-
return regex.test(value) ? null : {
|
|
13
|
+
return regex.test(value) ? null : { [key]: { requiredPattern: regex.toString(), actualValue: value } };
|
|
14
14
|
} else {
|
|
15
15
|
return null;
|
|
16
16
|
}
|
|
@@ -5,14 +5,14 @@ import { FormValidator, FormValue } from '../types';
|
|
|
5
5
|
/**
|
|
6
6
|
* a required dependant on another field
|
|
7
7
|
*/
|
|
8
|
-
export function RequiredIfValidator(condition: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => boolean): FormValidator {
|
|
8
|
+
export function RequiredIfValidator(condition: (value: FormValue, rootValue: FormValue, control?: FormAbstract, root?: FormAbstract) => boolean, key: string = 'required'): FormValidator {
|
|
9
9
|
return function (value, rootValue, control, root) {
|
|
10
10
|
// console.log('RequiredIfValidator', value, Boolean(condition(value, rootValue, control, root)));
|
|
11
11
|
if (Boolean(condition(value, rootValue, control, root)) === true) {
|
|
12
12
|
if (isArray(value) || typeof value === 'string') {
|
|
13
|
-
return (value.length === 0) ? {
|
|
13
|
+
return (value.length === 0) ? { [key]: true } : null;
|
|
14
14
|
} else {
|
|
15
|
-
return value == null ? {
|
|
15
|
+
return value == null ? { [key]: true } : null;
|
|
16
16
|
}
|
|
17
17
|
} else {
|
|
18
18
|
return null;
|
|
@@ -3,9 +3,9 @@ import { FormValidator } from '../types';
|
|
|
3
3
|
/**
|
|
4
4
|
* a required and true validator
|
|
5
5
|
*/
|
|
6
|
-
export function RequiredTrueValidator(): FormValidator {
|
|
6
|
+
export function RequiredTrueValidator(key: string = 'required'): FormValidator {
|
|
7
7
|
return function (value) {
|
|
8
8
|
// console.log('RequiredTrueValidator', value, value === true ? null : { required: true });
|
|
9
|
-
return value === true ? null : {
|
|
9
|
+
return value === true ? null : { [key]: true };
|
|
10
10
|
};
|
|
11
11
|
}
|
|
@@ -4,13 +4,13 @@ import { FormValidator } from '../types';
|
|
|
4
4
|
/**
|
|
5
5
|
* a required validator
|
|
6
6
|
*/
|
|
7
|
-
export function RequiredValidator(): FormValidator {
|
|
7
|
+
export function RequiredValidator(key: string = 'required'): FormValidator {
|
|
8
8
|
return function (value) {
|
|
9
9
|
// console.log('RequiredValidator', value, (value == null || value.length === 0) ? { required: true } : null);
|
|
10
10
|
if (isArray(value) || typeof value === 'string') {
|
|
11
|
-
return (value.length === 0) ? {
|
|
11
|
+
return (value.length === 0) ? { [key]: true } : null;
|
|
12
12
|
} else {
|
|
13
|
-
return value == null ? {
|
|
13
|
+
return value == null ? { [key]: true } : null;
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
16
|
// return (value == null || value.length === 0) ? 'required' : null;
|