@@ -11,10 +11,20 @@ describe("validate.string.length()", () => {
11
11
expect ( validate . string . length ( "hello" , 5 , 10 ) ) . to . equal ( "hello" ) ;
12
12
} ) ;
13
13
14
+ it ( "should validate strings that meet the exact length" , ( ) => {
15
+ expect ( validate . string . length ( "a" , 1 ) ) . to . equal ( "a" ) ;
16
+ expect ( validate . string . length ( "Hello, world!" , 13 ) ) . to . equal ( "Hello, world!" ) ;
17
+ expect ( validate . string . length ( "hello" , 5 ) ) . to . equal ( "hello" ) ;
18
+ } ) ;
19
+
14
20
it ( "should validate default values" , ( ) => {
15
21
expect ( validate . string . length ( undefined , 0 , 1 , "name" , "A" ) ) . to . equal ( "A" ) ;
16
22
expect ( validate . string . length ( undefined , 10 , 50 , "name" , "Hello, world!" ) ) . to . equal ( "Hello, world!" ) ;
17
23
expect ( validate . string . length ( undefined , 5 , 10 , "name" , "hello" ) ) . to . equal ( "hello" ) ;
24
+
25
+ expect ( validate . string . length ( undefined , 1 , "name" , "A" ) ) . to . equal ( "A" ) ;
26
+ expect ( validate . string . length ( undefined , 13 , "name" , "Hello, world!" ) ) . to . equal ( "Hello, world!" ) ;
27
+ expect ( validate . string . length ( undefined , 5 , "name" , "hello" ) ) . to . equal ( "hello" ) ;
18
28
} ) ;
19
29
20
30
it ( "should throw an error for strings that don't meet the minimum and maximum" , ( ) => {
@@ -29,6 +39,18 @@ describe("validate.string.length()", () => {
29
39
expect ( tooLong ( "Hello, world!" , 1 , 10 ) ) . to . throw ( RangeError , 'Invalid value: "Hello, world!". It cannot be more than 10 characters.' ) ;
30
40
} ) ;
31
41
42
+ it ( "should throw an error for strings that don't meet the exact length" , ( ) => {
43
+ function tooLong ( value , length ) {
44
+ return ( ) => {
45
+ validate . string . length ( value , length ) ;
46
+ } ;
47
+ }
48
+
49
+ expect ( tooLong ( " " , 0 ) ) . to . throw ( RangeError , 'Invalid value: " ". It must be exactly 0 characters.' ) ;
50
+ expect ( tooLong ( "abc" , 1 ) ) . to . throw ( RangeError , 'Invalid value: "abc". It must be exactly 1 character.' ) ;
51
+ expect ( tooLong ( "Hello, world!" , 10 ) ) . to . throw ( RangeError , 'Invalid value: "Hello, world!". It must be exactly 10 characters.' ) ;
52
+ } ) ;
53
+
32
54
it ( "should throw an error for defaults that don't meet the maximum" , ( ) => {
33
55
function invalidDefault ( defaultValue , min , max ) {
34
56
return ( ) => {
@@ -41,4 +63,16 @@ describe("validate.string.length()", () => {
41
63
expect ( invalidDefault ( "Hello, world!" , 1 , 10 ) ) . to . throw ( RangeError , 'Invalid name: "Hello, world!". It cannot be more than 10 characters.' ) ;
42
64
} ) ;
43
65
66
+ it ( "should throw an error for defaults that don't meet the exact length" , ( ) => {
67
+ function invalidDefault ( defaultValue , length ) {
68
+ return ( ) => {
69
+ validate . string . length ( undefined , length , "name" , defaultValue ) ;
70
+ } ;
71
+ }
72
+
73
+ expect ( invalidDefault ( " " , 0 ) ) . to . throw ( RangeError , 'Invalid name: " ". It must be exactly 0 characters.' ) ;
74
+ expect ( invalidDefault ( "abc" , 1 ) ) . to . throw ( RangeError , 'Invalid name: "abc". It must be exactly 1 character.' ) ;
75
+ expect ( invalidDefault ( "Hello, world!" , 10 ) ) . to . throw ( RangeError , 'Invalid name: "Hello, world!". It must be exactly 10 characters.' ) ;
76
+ } ) ;
77
+
44
78
} ) ;
0 commit comments