@@ -73,48 +73,44 @@ $RefParser.parse = function(schema, options, callback) {
7373 * @returns {Promise } - The returned promise resolves with the parsed JSON schema object.
7474 */
7575$RefParser . prototype . parse = function ( schema , options , callback ) {
76- if ( _isFunction ( options ) ) {
77- callback = options ;
78- options = undefined ;
79- }
76+ var args = normalizeArgs ( arguments ) ;
8077
81- if ( schema && _isObject ( schema ) ) {
78+ if ( args . schema && _isObject ( args . schema ) ) {
8279 // The schema is an object, not a path/url
83- this . schema = schema ;
80+ this . schema = args . schema ;
8481 this . _basePath = '' ;
8582
86- util . doCallback ( callback , null , this . schema ) ;
83+ util . doCallback ( args . callback , null , this . schema ) ;
8784 return Promise . resolve ( this . schema ) ;
8885 }
8986
90- if ( ! schema || ! _isString ( schema ) ) {
91- var err = ono ( 'Expected a file path, URL, or object. Got %s' , schema ) ;
92- util . doCallback ( callback , err , schema ) ;
87+ if ( ! args . schema || ! _isString ( args . schema ) ) {
88+ var err = ono ( 'Expected a file path, URL, or object. Got %s' , args . schema ) ;
89+ util . doCallback ( args . callback , err , args . schema ) ;
9390 return Promise . reject ( err ) ;
9491 }
9592
96- options = new Options ( options ) ;
9793 var me = this ;
9894
9995 // Resolve the absolute path of the schema
100- schema = url . resolve ( util . cwd ( ) , schema ) ;
101- this . _basePath = util . stripHash ( schema ) ;
96+ args . schema = url . resolve ( util . cwd ( ) , args . schema ) ;
97+ this . _basePath = util . stripHash ( args . schema ) ;
10298
10399 // Read the schema file/url
104- return read ( schema , this , options )
100+ return read ( args . schema , this . $refs , args . options )
105101 . then ( function ( $ref ) {
106102 // Make sure the file was a POJO (in JSON or YAML format), NOT a Buffer or string
107103 if ( $ref . value && _isPlainObject ( $ref . value ) ) {
108104 me . schema = $ref . value ;
109- util . doCallback ( callback , null , me . schema ) ;
105+ util . doCallback ( args . callback , null , me . schema ) ;
110106 return me . schema ;
111107 }
112108 else {
113109 throw ono . syntax ( '"%s" is not a valid JSON Schema' , me . _basePath ) ;
114110 }
115111 } )
116112 . catch ( function ( err ) {
117- util . doCallback ( callback , err ) ;
113+ util . doCallback ( args . callback , err ) ;
118114 return Promise . reject ( err ) ;
119115 } ) ;
120116} ;
@@ -149,24 +145,19 @@ $RefParser.resolve = function(schema, options, callback) {
149145 * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
150146 */
151147$RefParser . prototype . resolve = function ( schema , options , callback ) {
152- if ( _isFunction ( options ) ) {
153- callback = options ;
154- options = undefined ;
155- }
156-
157- options = new Options ( options ) ;
158148 var me = this ;
149+ var args = normalizeArgs ( arguments ) ;
159150
160- return this . parse ( schema , options )
151+ return this . parse ( args . schema , args . options )
161152 . then ( function ( ) {
162- return resolve ( me , options ) ;
153+ return resolve ( me , args . options ) ;
163154 } )
164155 . then ( function ( ) {
165- util . doCallback ( callback , null , me . $refs ) ;
156+ util . doCallback ( args . callback , null , me . $refs ) ;
166157 return me . $refs ;
167158 } )
168159 . catch ( function ( err ) {
169- util . doCallback ( callback , err , me . $refs ) ;
160+ util . doCallback ( args . callback , err , me . $refs ) ;
170161 return Promise . reject ( err ) ;
171162 } ) ;
172163} ;
@@ -197,22 +188,17 @@ $RefParser.bundle = function(schema, options, callback) {
197188 * @returns {Promise } - The returned promise resolves with the bundled JSON schema object.
198189 */
199190$RefParser . prototype . bundle = function ( schema , options , callback ) {
200- if ( _isFunction ( options ) ) {
201- callback = options ;
202- options = undefined ;
203- }
204-
205- options = new Options ( options ) ;
206191 var me = this ;
192+ var args = normalizeArgs ( arguments ) ;
207193
208- return this . resolve ( schema , options )
194+ return this . resolve ( args . schema , args . options )
209195 . then ( function ( ) {
210- bundle ( me , options ) ;
211- util . doCallback ( callback , null , me . schema ) ;
196+ bundle ( me , args . options ) ;
197+ util . doCallback ( args . callback , null , me . schema ) ;
212198 return me . schema ;
213199 } )
214200 . catch ( function ( err ) {
215- util . doCallback ( callback , err , me . schema ) ;
201+ util . doCallback ( args . callback , err , me . schema ) ;
216202 return Promise . reject ( err ) ;
217203 } ) ;
218204} ;
@@ -241,22 +227,39 @@ $RefParser.dereference = function(schema, options, callback) {
241227 * @returns {Promise } - The returned promise resolves with the dereferenced JSON schema object.
242228 */
243229$RefParser . prototype . dereference = function ( schema , options , callback ) {
244- if ( _isFunction ( options ) ) {
245- callback = options ;
246- options = undefined ;
247- }
248-
249- options = new Options ( options ) ;
250230 var me = this ;
231+ var args = normalizeArgs ( arguments ) ;
251232
252- return this . resolve ( schema , options )
233+ return this . resolve ( args . schema , args . options )
253234 . then ( function ( ) {
254- dereference ( me , options ) ;
255- util . doCallback ( callback , null , me . schema ) ;
235+ dereference ( me , args . options ) ;
236+ util . doCallback ( args . callback , null , me . schema ) ;
256237 return me . schema ;
257238 } )
258239 . catch ( function ( err ) {
259- util . doCallback ( callback , err , me . schema ) ;
240+ util . doCallback ( args . callback , err , me . schema ) ;
260241 return Promise . reject ( err ) ;
261242 } ) ;
262243} ;
244+
245+ /**
246+ * Normalizes the given arguments, accounting for optional args.
247+ *
248+ * @param {Arguments } args
249+ * @returns {object }
250+ */
251+ function normalizeArgs ( args ) {
252+ var options = args [ 1 ] , callback = args [ 2 ] ;
253+ if ( _isFunction ( options ) ) {
254+ callback = options ;
255+ options = undefined ;
256+ }
257+ if ( ! ( options instanceof Options ) ) {
258+ options = new Options ( options ) ;
259+ }
260+ return {
261+ schema : args [ 0 ] ,
262+ options : options ,
263+ callback : callback
264+ } ;
265+ }
0 commit comments