@@ -19,87 +19,66 @@ describe('Schema validation', () => {
19
19
addFormats ( validator ) ;
20
20
} ) ;
21
21
22
- describe ( 'Service schema' , ( ) => {
23
- describe ( 'with valid declarations' , ( ) => {
24
- const validDir = path . join ( fixturesDir , 'valid' ) ;
25
- const files = fs . readdirSync ( validDir ) ;
26
- const validFiles = files . filter ( file => file . endsWith ( '.json' ) ) ;
22
+ const loadFixture = filePath => {
23
+ const content = fs . readFileSync ( filePath , 'utf8' ) ;
27
24
28
- validFiles . forEach ( file => {
29
- it ( `validates ${ file } ` , ( ) => {
30
- const filePath = path . join ( validDir , file ) ;
31
- const content = fs . readFileSync ( filePath , 'utf8' ) ;
32
- const declaration = JSON . parse ( content ) ;
25
+ return JSON . parse ( content ) ;
26
+ } ;
33
27
34
- const valid = validator . validate ( serviceSchema , declaration ) ;
28
+ const getJsonFiles = dir => {
29
+ const files = fs . readdirSync ( dir ) ;
35
30
36
- if ( ! valid ) {
37
- console . error ( `Validation errors for ${ file } :` , validator . errors ) ;
38
- }
31
+ return files . filter ( file => file . endsWith ( '.json' ) ) ;
32
+ } ;
39
33
40
- expect ( valid , `${ file } should be valid` ) . to . be . true ;
41
- } ) ;
42
- } ) ;
43
- } ) ;
34
+ const validateDeclaration = ( schema , declaration , file , shouldBeValid ) => {
35
+ const valid = validator . validate ( schema , declaration ) ;
44
36
45
- describe ( 'with invalid declarations' , ( ) => {
46
- const invalidDir = path . join ( fixturesDir , 'invalid' ) ;
47
- const files = fs . readdirSync ( invalidDir ) ;
48
- const invalidFiles = files . filter ( file => file . endsWith ( '.json' ) ) ;
37
+ if ( ! valid && shouldBeValid ) {
38
+ console . error ( `Validation errors for ${ file } :` , validator . errors ) ;
39
+ }
40
+
41
+ return valid ;
42
+ } ;
49
43
50
- invalidFiles . forEach ( file => {
51
- it ( `rejects ${ file } ` , ( ) => {
52
- const filePath = path . join ( invalidDir , file ) ;
53
- const content = fs . readFileSync ( filePath , 'utf8' ) ;
54
- const declaration = JSON . parse ( content ) ;
44
+ const createValidationTests = ( schema , directory , shouldBeValid ) => {
45
+ const files = getJsonFiles ( directory ) ;
46
+ const action = shouldBeValid ? 'validates' : 'rejects' ;
55
47
56
- const valid = validator . validate ( serviceSchema , declaration ) ;
48
+ files . forEach ( file => {
49
+ it ( `${ action } ${ file } ` , ( ) => {
50
+ const filePath = path . join ( directory , file ) ;
51
+ const declaration = loadFixture ( filePath ) ;
52
+ const valid = validateDeclaration ( schema , declaration , file , shouldBeValid ) ;
57
53
58
- expect ( valid , `${ file } should be invalid` ) . to . be . false ;
59
- } ) ;
54
+ expect ( valid , `${ file } should be ${ shouldBeValid ? 'valid' : 'invalid' } ` ) . to . equal ( shouldBeValid ) ;
60
55
} ) ;
61
56
} ) ;
62
- } ) ;
57
+ } ;
63
58
64
- describe ( 'Service history schema' , ( ) => {
65
- describe ( 'with valid history declarations' , ( ) => {
66
- const validHistoryDir = path . join ( fixturesDir , 'valid-history' ) ;
67
- const files = fs . readdirSync ( validHistoryDir ) ;
68
- const validHistoryFiles = files . filter ( file => file . endsWith ( '.json' ) ) ;
59
+ describe ( 'Service schema' , ( ) => {
60
+ const validDir = path . join ( fixturesDir , 'valid' ) ;
61
+ const invalidDir = path . join ( fixturesDir , 'invalid' ) ;
69
62
70
- validHistoryFiles . forEach ( file => {
71
- it ( `validates ${ file } ` , ( ) => {
72
- const filePath = path . join ( validHistoryDir , file ) ;
73
- const content = fs . readFileSync ( filePath , 'utf8' ) ;
74
- const declaration = JSON . parse ( content ) ;
63
+ describe ( 'with valid declarations' , ( ) => {
64
+ createValidationTests ( serviceSchema , validDir , true ) ;
65
+ } ) ;
75
66
76
- const valid = validator . validate ( serviceHistorySchema , declaration ) ;
67
+ describe ( 'with invalid declarations' , ( ) => {
68
+ createValidationTests ( serviceSchema , invalidDir , false ) ;
69
+ } ) ;
70
+ } ) ;
77
71
78
- if ( ! valid ) {
79
- console . error ( `Validation errors for ${ file } :` , validator . errors ) ;
80
- }
72
+ describe ( 'Service history schema' , ( ) => {
73
+ const validHistoryDir = path . join ( fixturesDir , 'valid-history' ) ;
74
+ const invalidHistoryDir = path . join ( fixturesDir , 'invalid-history' ) ;
81
75
82
- expect ( valid , `${ file } should be valid` ) . to . be . true ;
83
- } ) ;
84
- } ) ;
76
+ describe ( 'with valid history declarations' , ( ) => {
77
+ createValidationTests ( serviceHistorySchema , validHistoryDir , true ) ;
85
78
} ) ;
86
79
87
80
describe ( 'with invalid history declarations' , ( ) => {
88
- const invalidHistoryDir = path . join ( fixturesDir , 'invalid-history' ) ;
89
- const files = fs . readdirSync ( invalidHistoryDir ) ;
90
- const invalidHistoryFiles = files . filter ( file => file . endsWith ( '.json' ) ) ;
91
-
92
- invalidHistoryFiles . forEach ( file => {
93
- it ( `rejects ${ file } ` , ( ) => {
94
- const filePath = path . join ( invalidHistoryDir , file ) ;
95
- const content = fs . readFileSync ( filePath , 'utf8' ) ;
96
- const declaration = JSON . parse ( content ) ;
97
-
98
- const valid = validator . validate ( serviceHistorySchema , declaration ) ;
99
-
100
- expect ( valid , `${ file } should be invalid` ) . to . be . false ;
101
- } ) ;
102
- } ) ;
81
+ createValidationTests ( serviceHistorySchema , invalidHistoryDir , false ) ;
103
82
} ) ;
104
83
} ) ;
105
84
} ) ;
0 commit comments