|
6 | 6 | Validate, |
7 | 7 | ValidateNested, |
8 | 8 | ValidatorConstraint, |
| 9 | + IsOptional, |
| 10 | + IsNotEmpty, |
9 | 11 | } from '../../src/decorator/decorators'; |
10 | 12 | import { Validator } from '../../src/validation/Validator'; |
11 | 13 | import { |
@@ -863,6 +865,112 @@ describe('groups', () => { |
863 | 865 | }); |
864 | 866 | }); |
865 | 867 |
|
| 868 | + describe('ValidationOptions.always', function () { |
| 869 | + class MyClass { |
| 870 | + @Contains('noOptions') |
| 871 | + noOptions: string; |
| 872 | + |
| 873 | + @Contains('groupA', { |
| 874 | + groups: ['A'], |
| 875 | + }) |
| 876 | + groupA: string; |
| 877 | + |
| 878 | + @Contains('alwaysFalse', { |
| 879 | + always: false, |
| 880 | + }) |
| 881 | + alwaysFalse: string; |
| 882 | + |
| 883 | + @Contains('alwaysTrue', { |
| 884 | + always: true, |
| 885 | + }) |
| 886 | + alwaysTrue: string; |
| 887 | + } |
| 888 | + |
| 889 | + const model1 = new MyClass(); |
| 890 | + model1.noOptions = 'XXX'; |
| 891 | + model1.groupA = 'groupA'; |
| 892 | + model1.alwaysFalse = 'alwaysFalse'; |
| 893 | + model1.alwaysTrue = 'alwaysTrue'; |
| 894 | + |
| 895 | + const model2 = new MyClass(); |
| 896 | + model2.noOptions = 'noOptions'; |
| 897 | + model2.groupA = 'XXX'; |
| 898 | + model2.alwaysFalse = 'alwaysFalse'; |
| 899 | + model2.alwaysTrue = 'alwaysTrue'; |
| 900 | + |
| 901 | + const model3 = new MyClass(); |
| 902 | + model3.noOptions = 'noOptions'; |
| 903 | + model3.groupA = 'groupA'; |
| 904 | + model3.alwaysFalse = 'XXX'; |
| 905 | + model3.alwaysTrue = 'alwaysTrue'; |
| 906 | + |
| 907 | + const model4 = new MyClass(); |
| 908 | + model4.noOptions = 'noOptions'; |
| 909 | + model4.groupA = 'groupA'; |
| 910 | + model4.alwaysFalse = 'alwaysFalse'; |
| 911 | + model4.alwaysTrue = 'XXX'; |
| 912 | + |
| 913 | + it('should validate decorator without options', function () { |
| 914 | + return validator.validate(model1, { always: true, groups: ['A'] }).then(errors => { |
| 915 | + expect(errors).toHaveLength(1); |
| 916 | + }); |
| 917 | + }); |
| 918 | + |
| 919 | + it('should not validate decorator with groups if validating without matching groups', function () { |
| 920 | + return validator.validate(model2, { always: true, groups: ['B'] }).then(errors => { |
| 921 | + expect(errors).toHaveLength(0); |
| 922 | + }); |
| 923 | + }); |
| 924 | + |
| 925 | + it('should not validate decorator with always set to false', function () { |
| 926 | + return validator.validate(model3, { always: true, groups: ['A'] }).then(errors => { |
| 927 | + expect(errors).toHaveLength(0); |
| 928 | + }); |
| 929 | + }); |
| 930 | + |
| 931 | + it('should validate decorator with always set to true', function () { |
| 932 | + return validator.validate(model4, { always: true, groups: ['A'] }).then(errors => { |
| 933 | + expect(errors).toHaveLength(1); |
| 934 | + }); |
| 935 | + }); |
| 936 | + }); |
| 937 | + |
| 938 | + describe('strictGroups', function () { |
| 939 | + class MyClass { |
| 940 | + @Contains('hello', { |
| 941 | + groups: ['A'], |
| 942 | + }) |
| 943 | + title: string; |
| 944 | + } |
| 945 | + |
| 946 | + const model1 = new MyClass(); |
| 947 | + |
| 948 | + it('should ignore decorators with groups if validating without groups', function () { |
| 949 | + return validator.validate(model1, { strictGroups: true }).then(errors => { |
| 950 | + expect(errors).toHaveLength(0); |
| 951 | + }); |
| 952 | + }); |
| 953 | + |
| 954 | + it('should ignore decorators with groups if validating with empty groups array', function () { |
| 955 | + return validator.validate(model1, { strictGroups: true, groups: [] }).then(errors => { |
| 956 | + expect(errors).toHaveLength(0); |
| 957 | + }); |
| 958 | + }); |
| 959 | + |
| 960 | + it('should include decorators with groups if validating with matching groups', function () { |
| 961 | + return validator.validate(model1, { strictGroups: true, groups: ['A'] }).then(errors => { |
| 962 | + expect(errors).toHaveLength(1); |
| 963 | + expectTitleContains(errors[0]); |
| 964 | + }); |
| 965 | + }); |
| 966 | + |
| 967 | + it('should not include decorators with groups if validating with different groups', function () { |
| 968 | + return validator.validate(model1, { strictGroups: true, groups: ['B'] }).then(errors => { |
| 969 | + expect(errors).toHaveLength(0); |
| 970 | + }); |
| 971 | + }); |
| 972 | + }); |
| 973 | + |
866 | 974 | describe('always', () => { |
867 | 975 | class MyClass { |
868 | 976 | @Contains('hello', { |
|
0 commit comments