@@ -8,11 +8,9 @@ describe('Reporter', () => {
8
8
it ( 'should format a ValidationError to human readable message' , ( ) => {
9
9
const targetProperty = 'targetProperty' ;
10
10
const value = undefined ;
11
- const error = new PropertyValidationError ( { type : 'string ' , value } ) ;
11
+ const error = new PropertyValidationError ( { expect : 'message ' , value } ) ;
12
12
const message = defaultFormatter ( { targetProperty, ...error } ) ;
13
- expect ( message ) . toEqual (
14
- `Invalid value ${ value } supplied to : [⚠️ Schema With Type] at property ${ targetProperty } . Expecting type ${ error . type } `
15
- ) ;
13
+ expect ( message ) . toEqual ( `Invalid value ${ value } supplied at property ${ targetProperty } . Expecting: ${ error . expect } ` ) ;
16
14
} ) ;
17
15
} ) ;
18
16
describe ( 'Validation' , ( ) => {
@@ -32,8 +30,8 @@ describe('Reporter', () => {
32
30
} ) ;
33
31
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
34
32
const errors = reporter . report ( result ) ;
35
- const error1 = new PropertyValidationError ( { type : 'boolean' , value : undefined } ) ;
36
- const error2 = new PropertyValidationError ( { type : 'number' , value : undefined } ) ;
33
+ const error1 = new PropertyValidationError ( { expect : 'value to be typeof boolean' , value : undefined } ) ;
34
+ const error2 = new PropertyValidationError ( { expect : 'value to be typeof number' , value : undefined } ) ;
37
35
38
36
const message1 = defaultFormatter ( { targetProperty : 't1' , ...error1 } ) ;
39
37
const message2 = defaultFormatter ( { targetProperty : 't2' , ...error2 } ) ;
@@ -45,6 +43,14 @@ describe('Reporter', () => {
45
43
}
46
44
} ) ;
47
45
46
+ it ( 'should throw an exception when trying to use a rule more than once' , ( ) => {
47
+ expect ( ( ) => {
48
+ Validation . string ( )
49
+ . max ( 1 )
50
+ . max ( 1 ) ;
51
+ } ) . toThrow ( 'Rule max has already been used' ) ;
52
+ } ) ;
53
+
48
54
describe ( 'string' , ( ) => {
49
55
it ( 'should report error on string undefined' , ( ) => {
50
56
interface S {
@@ -56,7 +62,7 @@ describe('Reporter', () => {
56
62
57
63
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . string ( ) } } ) ;
58
64
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
59
- const error = new PropertyValidationError ( { type : 'string' , value : undefined } ) ;
65
+ const error = new PropertyValidationError ( { expect : 'value to be typeof string' , value : undefined } ) ;
60
66
const message = defaultFormatter ( { targetProperty : 't1' , ...error } ) ;
61
67
const errors = reporter . report ( result ) ;
62
68
expect ( errors ) . not . toBeNull ( ) ;
@@ -65,6 +71,68 @@ describe('Reporter', () => {
65
71
expect ( errors [ 0 ] ) . toBe ( message ) ;
66
72
}
67
73
} ) ;
74
+
75
+ it ( 'should report error when string max length is not met' , ( ) => {
76
+ interface S {
77
+ s1 : string ;
78
+ }
79
+ interface T {
80
+ t1 : string ;
81
+ }
82
+
83
+ const schema = createSchema < T , S > ( { t1 : { fn : value => value . s1 , validation : Validation . string ( ) . max ( 3 ) } } ) ;
84
+ const result = morphism ( schema , { s1 : 'value' } ) ;
85
+ const error = new PropertyValidationError ( { expect : `value to be less or equal than 3` , value : 'value' } ) ;
86
+ const message = defaultFormatter ( { targetProperty : 't1' , ...error } ) ;
87
+ const errors = reporter . report ( result ) ;
88
+ expect ( errors ) . not . toBeNull ( ) ;
89
+ if ( errors ) {
90
+ expect ( errors . length ) . toEqual ( 1 ) ;
91
+ expect ( errors [ 0 ] ) . toBe ( message ) ;
92
+ }
93
+ } ) ;
94
+
95
+ it ( 'should report error when string min length is not met' , ( ) => {
96
+ interface S {
97
+ s1 : string ;
98
+ }
99
+ interface T {
100
+ t1 : string ;
101
+ }
102
+
103
+ const schema = createSchema < T , S > ( { t1 : { fn : value => value . s1 , validation : Validation . string ( ) . min ( 3 ) } } ) ;
104
+ const result = morphism ( schema , { s1 : 'a' } ) ;
105
+ const error = new PropertyValidationError ( { expect : `value to be greater or equal than 3` , value : 'a' } ) ;
106
+ const message = defaultFormatter ( { targetProperty : 't1' , ...error } ) ;
107
+ const errors = reporter . report ( result ) ;
108
+ expect ( errors ) . not . toBeNull ( ) ;
109
+ if ( errors ) {
110
+ expect ( errors . length ) . toEqual ( 1 ) ;
111
+ expect ( errors [ 0 ] ) . toBe ( message ) ;
112
+ }
113
+ } ) ;
114
+
115
+ it ( 'should return the value when the validation pass' , ( ) => {
116
+ interface S {
117
+ s1 : string ;
118
+ }
119
+ interface T {
120
+ t1 : string ;
121
+ }
122
+
123
+ const schema = createSchema < T , S > ( {
124
+ t1 : {
125
+ fn : value => value . s1 ,
126
+ validation : Validation . string ( )
127
+ . min ( 1 )
128
+ . max ( 3 )
129
+ }
130
+ } ) ;
131
+ const result = morphism ( schema , { s1 : 'aaa' } ) ;
132
+ const errors = reporter . report ( result ) ;
133
+ expect ( errors ) . toBeNull ( ) ;
134
+ expect ( result . t1 ) . toBe ( 'aaa' ) ;
135
+ } ) ;
68
136
} ) ;
69
137
70
138
describe ( 'number' , ( ) => {
@@ -78,7 +146,7 @@ describe('Reporter', () => {
78
146
79
147
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . number ( ) } } ) ;
80
148
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
81
- const error = new PropertyValidationError ( { type : 'number' , value : undefined } ) ;
149
+ const error = new PropertyValidationError ( { expect : 'value to be typeof number' , value : undefined } ) ;
82
150
const message = defaultFormatter ( { targetProperty : 't1' , ...error } ) ;
83
151
const errors = reporter . report ( result ) ;
84
152
expect ( errors ) . not . toBeNull ( ) ;
@@ -159,7 +227,7 @@ describe('Reporter', () => {
159
227
160
228
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . boolean ( ) } } ) ;
161
229
const result = morphism ( schema , JSON . parse ( '{ "s1": "a value" }' ) ) ;
162
- const error = new PropertyValidationError ( { type : 'boolean' , value : 'a value' } ) ;
230
+ const error = new PropertyValidationError ( { expect : 'value to be typeof boolean' , value : 'a value' } ) ;
163
231
const message = defaultFormatter ( { targetProperty : 't1' , ...error } ) ;
164
232
165
233
const errors = reporter . report ( result ) ;
0 commit comments