1
1
import { morphism , createSchema } from '../morphism' ;
2
- import { defaultFormatter , reporter } from './reporter' ;
3
- import { PropertyValidationError } from './PropertyValidationError' ;
2
+ import { defaultFormatter , reporter , ValidationError } from './reporter' ;
4
3
import { Validation } from './Validation' ;
5
4
6
5
describe ( 'Reporter' , ( ) => {
7
6
describe ( 'Formatter' , ( ) => {
8
7
it ( 'should format a ValidationError to human readable message' , ( ) => {
9
8
const targetProperty = 'targetProperty' ;
10
9
const value = undefined ;
11
- const error = new PropertyValidationError ( { expect : 'message' , value } ) ;
12
- const message = defaultFormatter ( { targetProperty , ... error } ) ;
10
+ const error = new ValidationError ( { targetProperty , expect : 'message' , value } ) ;
11
+ const message = defaultFormatter ( error ) ;
13
12
expect ( message ) . toEqual ( `Invalid value ${ value } supplied at property ${ targetProperty } . Expecting: ${ error . expect } ` ) ;
14
13
} ) ;
15
14
} ) ;
@@ -30,11 +29,11 @@ describe('Reporter', () => {
30
29
} ) ;
31
30
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
32
31
const errors = reporter . report ( result ) ;
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 } ) ;
32
+ const error1 = new ValidationError ( { targetProperty : 't1' , expect : 'value to be typeof boolean' , value : undefined } ) ;
33
+ const error2 = new ValidationError ( { targetProperty : 't2' , expect : 'value to be typeof number' , value : undefined } ) ;
35
34
36
- const message1 = defaultFormatter ( { targetProperty : 't1' , ... error1 } ) ;
37
- const message2 = defaultFormatter ( { targetProperty : 't2' , ... error2 } ) ;
35
+ const message1 = defaultFormatter ( error1 ) ;
36
+ const message2 = defaultFormatter ( error2 ) ;
38
37
expect ( errors ) . not . toBeNull ( ) ;
39
38
if ( errors ) {
40
39
expect ( errors . length ) . toEqual ( 2 ) ;
@@ -62,8 +61,8 @@ describe('Reporter', () => {
62
61
63
62
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . string ( ) } } ) ;
64
63
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
65
- const error = new PropertyValidationError ( { expect : 'value to be typeof string' , value : undefined } ) ;
66
- const message = defaultFormatter ( { targetProperty : 't1' , ... error } ) ;
64
+ const error = new ValidationError ( { targetProperty : 't1' , expect : 'value to be typeof string' , value : undefined } ) ;
65
+ const message = defaultFormatter ( error ) ;
67
66
const errors = reporter . report ( result ) ;
68
67
expect ( errors ) . not . toBeNull ( ) ;
69
68
if ( errors ) {
@@ -82,8 +81,8 @@ describe('Reporter', () => {
82
81
83
82
const schema = createSchema < T , S > ( { t1 : { fn : value => value . s1 , validation : Validation . string ( ) . max ( 3 ) } } ) ;
84
83
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 } ) ;
84
+ const error = new ValidationError ( { targetProperty : 't1' , expect : `value to be less or equal than 3` , value : 'value' } ) ;
85
+ const message = defaultFormatter ( error ) ;
87
86
const errors = reporter . report ( result ) ;
88
87
expect ( errors ) . not . toBeNull ( ) ;
89
88
if ( errors ) {
@@ -102,8 +101,8 @@ describe('Reporter', () => {
102
101
103
102
const schema = createSchema < T , S > ( { t1 : { fn : value => value . s1 , validation : Validation . string ( ) . min ( 3 ) } } ) ;
104
103
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 } ) ;
104
+ const error = new ValidationError ( { targetProperty : 't1' , expect : `value to be greater or equal than 3` , value : 'a' } ) ;
105
+ const message = defaultFormatter ( error ) ;
107
106
const errors = reporter . report ( result ) ;
108
107
expect ( errors ) . not . toBeNull ( ) ;
109
108
if ( errors ) {
@@ -146,8 +145,8 @@ describe('Reporter', () => {
146
145
147
146
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . number ( ) } } ) ;
148
147
const result = morphism ( schema , JSON . parse ( '{}' ) ) ;
149
- const error = new PropertyValidationError ( { expect : 'value to be typeof number' , value : undefined } ) ;
150
- const message = defaultFormatter ( { targetProperty : 't1' , ... error } ) ;
148
+ const error = new ValidationError ( { targetProperty : 't1' , expect : 'value to be typeof number' , value : undefined } ) ;
149
+ const message = defaultFormatter ( error ) ;
151
150
const errors = reporter . report ( result ) ;
152
151
expect ( errors ) . not . toBeNull ( ) ;
153
152
if ( errors ) {
@@ -227,8 +226,8 @@ describe('Reporter', () => {
227
226
228
227
const schema = createSchema < T , S > ( { t1 : { path : 's1' , fn : val => val , validation : Validation . boolean ( ) } } ) ;
229
228
const result = morphism ( schema , JSON . parse ( '{ "s1": "a value" }' ) ) ;
230
- const error = new PropertyValidationError ( { expect : 'value to be typeof boolean' , value : 'a value' } ) ;
231
- const message = defaultFormatter ( { targetProperty : 't1' , ... error } ) ;
229
+ const error = new ValidationError ( { targetProperty : 't1' , expect : 'value to be typeof boolean' , value : 'a value' } ) ;
230
+ const message = defaultFormatter ( error ) ;
232
231
233
232
const errors = reporter . report ( result ) ;
234
233
0 commit comments