@@ -19,87 +19,66 @@ describe('Schema validation', () => {
1919 addFormats ( validator ) ;
2020 } ) ;
2121
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' ) ;
2724
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+ } ;
3327
34- const valid = validator . validate ( serviceSchema , declaration ) ;
28+ const getJsonFiles = dir => {
29+ const files = fs . readdirSync ( dir ) ;
3530
36- if ( ! valid ) {
37- console . error ( `Validation errors for ${ file } :` , validator . errors ) ;
38- }
31+ return files . filter ( file => file . endsWith ( '.json' ) ) ;
32+ } ;
3933
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 ) ;
4436
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+ } ;
4943
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' ;
5547
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 ) ;
5753
58- expect ( valid , `${ file } should be invalid` ) . to . be . false ;
59- } ) ;
54+ expect ( valid , `${ file } should be ${ shouldBeValid ? 'valid' : 'invalid' } ` ) . to . equal ( shouldBeValid ) ;
6055 } ) ;
6156 } ) ;
62- } ) ;
57+ } ;
6358
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' ) ;
6962
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+ } ) ;
7566
76- const valid = validator . validate ( serviceHistorySchema , declaration ) ;
67+ describe ( 'with invalid declarations' , ( ) => {
68+ createValidationTests ( serviceSchema , invalidDir , false ) ;
69+ } ) ;
70+ } ) ;
7771
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' ) ;
8175
82- expect ( valid , `${ file } should be valid` ) . to . be . true ;
83- } ) ;
84- } ) ;
76+ describe ( 'with valid history declarations' , ( ) => {
77+ createValidationTests ( serviceHistorySchema , validHistoryDir , true ) ;
8578 } ) ;
8679
8780 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 ) ;
10382 } ) ;
10483 } ) ;
10584} ) ;
0 commit comments