@@ -43,19 +43,18 @@ describe("References to non-JSON files", () => {
4343 } ) ;
4444
4545 it ( 'should parse text as binary if "parse.text" is disabled' , async ( ) => {
46- const schema = await $RefParser
47- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
48- parse : {
49- // Disable the text parser
50- text : false ,
51- // Parse all non-YAML files as binary
52- binary : {
53- canParse ( file ) {
54- return file . url . substr ( - 5 ) !== ".yaml" ;
55- }
46+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
47+ parse : {
48+ // Disable the text parser
49+ text : false ,
50+ // Parse all non-YAML files as binary
51+ binary : {
52+ canParse ( file ) {
53+ return file . url . substr ( - 5 ) !== ".yaml" ;
5654 }
5755 }
58- } ) ;
56+ }
57+ } ) ;
5958 schema . definitions . markdown = helper . convertNodeBuffersToPOJOs ( schema . definitions . markdown ) ;
6059 schema . definitions . html = helper . convertNodeBuffersToPOJOs ( schema . definitions . html ) ;
6160 schema . definitions . css = helper . convertNodeBuffersToPOJOs ( schema . definitions . css ) ;
@@ -77,92 +76,87 @@ describe("References to non-JSON files", () => {
7776 } ) ;
7877
7978 it ( "should use a custom parser with static values" , async ( ) => {
80- const schema = await $RefParser
81- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
82- parse : {
83- // A custom parser that always returns the same value
84- staticParser : {
85- order : 201 ,
86- canParse : true ,
87- parse : "The quick brown fox jumped over the lazy dog"
88- }
79+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
80+ parse : {
81+ // A custom parser that always returns the same value
82+ staticParser : {
83+ order : 201 ,
84+ canParse : true ,
85+ parse : "The quick brown fox jumped over the lazy dog"
8986 }
90- } ) ;
87+ }
88+ } ) ;
9189 expect ( schema ) . to . deep . equal ( dereferencedSchema . staticParser ) ;
9290 } ) ;
9391
9492 it ( "should use a custom parser that returns a value" , async ( ) => {
95- const schema = await $RefParser
96- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
97- parse : {
98- // A custom parser that returns the contents of ".foo" files, in reverse
99- reverseFooParser : {
100- canParse ( file ) {
101- return file . url . substr ( - 4 ) === ".foo" ;
102- } ,
103- parse ( file ) {
104- return file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ;
105- }
93+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
94+ parse : {
95+ // A custom parser that returns the contents of ".foo" files, in reverse
96+ reverseFooParser : {
97+ canParse ( file ) {
98+ return file . url . substr ( - 4 ) === ".foo" ;
99+ } ,
100+ parse ( file ) {
101+ return file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ;
106102 }
107103 }
108- } ) ;
104+ }
105+ } ) ;
109106 schema . definitions . binary = helper . convertNodeBuffersToPOJOs ( schema . definitions . binary ) ;
110107 expect ( schema ) . to . deep . equal ( dereferencedSchema . customParser ) ;
111108 } ) ;
112109
113110 it ( "should use a custom parser that calls a callback" , async ( ) => {
114- const schema = await $RefParser
115- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
116- parse : {
117- // A custom parser that returns the contents of ".foo" files, in reverse
118- reverseFooParser : {
119- canParse : / \. F O O $ / i,
120- parse ( file , callback ) {
121- let reversed = file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ;
122- callback ( null , reversed ) ;
123- }
111+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
112+ parse : {
113+ // A custom parser that returns the contents of ".foo" files, in reverse
114+ reverseFooParser : {
115+ canParse : / \. F O O $ / i,
116+ parse ( file , callback ) {
117+ let reversed = file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ;
118+ callback ( null , reversed ) ;
124119 }
125120 }
126- } ) ;
121+ }
122+ } ) ;
127123 schema . definitions . binary = helper . convertNodeBuffersToPOJOs ( schema . definitions . binary ) ;
128124 expect ( schema ) . to . deep . equal ( dereferencedSchema . customParser ) ;
129125 } ) ;
130126
131127 it ( "should use a custom parser that returns a promise" , async ( ) => {
132- const schema = await $RefParser
133- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
134- parse : {
135- // A custom parser that returns the contents of ".foo" files, in reverse
136- reverseFooParser : {
137- canParse : [ ".foo" ] ,
138- parse ( file ) {
139- return new Promise ( function ( resolve , reject ) {
140- let reversed = file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ;
141- resolve ( reversed ) ;
142- } ) ;
143- }
128+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
129+ parse : {
130+ // A custom parser that returns the contents of ".foo" files, in reverse
131+ reverseFooParser : {
132+ canParse : [ ".foo" ] ,
133+ async parse ( file ) {
134+ let reversed = await new Promise ( ( resolve ) => {
135+ resolve ( file . data . toString ( ) . split ( "" ) . reverse ( ) . join ( "" ) ) ;
136+ } ) ;
137+ return reversed ;
144138 }
145139 }
146- } ) ;
140+ }
141+ } ) ;
147142 schema . definitions . binary = helper . convertNodeBuffersToPOJOs ( schema . definitions . binary ) ;
148143 expect ( schema ) . to . deep . equal ( dereferencedSchema . customParser ) ;
149144 } ) ;
150145
151146 it ( "should continue parsing if a custom parser fails" , async ( ) => {
152- const schema = await $RefParser
153- . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
154- parse : {
155- // A custom parser that always fails,
156- // so the built-in parsers will be used as a fallback
157- badParser : {
158- order : 1 ,
159- canParse : / \. ( m d | h t m l | c s s | p n g ) $ / i,
160- parse ( file , callback ) {
161- callback ( "BOMB!!!" ) ;
162- }
147+ const schema = await $RefParser . dereference ( path . rel ( "specs/parsers/parsers.yaml" ) , {
148+ parse : {
149+ // A custom parser that always fails,
150+ // so the built-in parsers will be used as a fallback
151+ badParser : {
152+ order : 1 ,
153+ canParse : / \. ( m d | h t m l | c s s | p n g ) $ / i,
154+ parse ( file , callback ) {
155+ callback ( "BOMB!!!" ) ;
163156 }
164157 }
165- } ) ;
158+ }
159+ } ) ;
166160 schema . definitions . binary = helper . convertNodeBuffersToPOJOs ( schema . definitions . binary ) ;
167161 expect ( schema ) . to . deep . equal ( dereferencedSchema . defaultParsers ) ;
168162 } ) ;
0 commit comments