88
99var Promise = require ( './promise' ) ,
1010 Options = require ( './options' ) ,
11- PathOrUrl = require ( './path-or-url' ) ,
1211 $Refs = require ( './refs' ) ,
1312 read = require ( './read' ) ,
1413 resolve = require ( './resolve' ) ,
1514 dereference = require ( './dereference' ) ,
1615 util = require ( './util' ) ,
16+ url = require ( 'url' ) ,
17+ ono = require ( 'ono' ) ,
1718 _cloneDeep = require ( 'lodash/lang/cloneDeep' ) ,
1819 _isFunction = require ( 'lodash/lang/isFunction' ) ,
1920 _isObject = require ( 'lodash/lang/isObject' ) ,
@@ -22,6 +23,12 @@ var Promise = require('./promise'),
2223
2324module . exports = $RefParser ;
2425
26+ /**
27+ * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
28+ * and provides methods for traversing, manipulating, and dereferencing those references.
29+ *
30+ * @constructor
31+ */
2532function $RefParser ( ) {
2633 /**
2734 * The parsed (and possibly dereferenced) JSON schema object
@@ -32,23 +39,43 @@ function $RefParser() {
3239 this . schema = null ;
3340
3441 /**
35- * The resolved $ref pointers
42+ * The resolved JSON references
3643 *
3744 * @type {$Refs }
3845 */
3946 this . $refs = new $Refs ( ) ;
4047
4148 /**
42- * @type {PathOrUrl }
49+ * @type {string }
4350 * @protected
4451 */
45- this . _base = null ;
52+ this . _basePath = '' ;
4653}
4754
55+ /**
56+ * Parses the given JSON schema.
57+ * This method does not resolve any JSON references.
58+ * It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
59+ *
60+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
61+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed
62+ * @param {function } [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
63+ * @returns {Promise } - The returned promise resolves with the parsed JSON schema object.
64+ */
4865$RefParser . parse = function ( schema , options , callback ) {
4966 return new $RefParser ( ) . parse ( schema , options , callback ) ;
5067} ;
5168
69+ /**
70+ * Parses the given JSON schema.
71+ * This method does not resolve any JSON references.
72+ * It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
73+ *
74+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
75+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed
76+ * @param {function } [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
77+ * @returns {Promise } - The returned promise resolves with the parsed JSON schema object.
78+ */
5279$RefParser . prototype . parse = function ( schema , options , callback ) {
5380 if ( _isFunction ( options ) ) {
5481 callback = options ;
@@ -58,26 +85,27 @@ $RefParser.prototype.parse = function(schema, options, callback) {
5885 if ( schema && _isObject ( schema ) ) {
5986 // The schema is an object, not a path/url
6087 this . schema = _cloneDeep ( schema ) ;
61- this . _base = PathOrUrl . cwd ( ) ;
88+ this . _basePath = '' ;
6289
6390 util . doCallback ( callback , null , this . schema ) ;
6491 return Promise . resolve ( this . schema ) ;
6592 }
6693
6794 if ( ! schema || ! _isString ( schema ) ) {
68- var err = util . newError ( 'Expected a file path, URL, or object. Got %s' , schema ) ;
95+ var err = ono ( 'Expected a file path, URL, or object. Got %s' , schema ) ;
6996 util . doCallback ( callback , err , schema ) ;
7097 return Promise . reject ( err ) ;
7198 }
7299
73100 options = new Options ( options ) ;
74101 var me = this ;
75102
76- // Resolve the full URL of the schema
77- this . _base = new PathOrUrl ( schema , { allowFileHash : true } ) ;
103+ // Resolve the absolute path of the schema
104+ schema = url . resolve ( util . cwd ( ) , schema ) ;
105+ this . _basePath = util . stripHash ( schema ) ;
78106
79107 // Read the schema file/url
80- return read ( this . _base , this , options )
108+ return read ( schema , this , options )
81109 . then ( function ( $ref ) {
82110 // Make sure the file was a POJO (in JSON or YAML format), NOT a Buffer or string
83111 if ( $ref . value && _isPlainObject ( $ref . value ) ) {
@@ -86,7 +114,7 @@ $RefParser.prototype.parse = function(schema, options, callback) {
86114 return me . schema ;
87115 }
88116 else {
89- throw util . newError ( SyntaxError , '"%s" is not a valid JSON Schema' , me . _base ) ;
117+ throw ono . syntax ( '"%s" is not a valid JSON Schema' , me . _basePath ) ;
90118 }
91119 } )
92120 . catch ( function ( err ) {
@@ -95,10 +123,34 @@ $RefParser.prototype.parse = function(schema, options, callback) {
95123 } ) ;
96124} ;
97125
126+ /**
127+ * Parses the given JSON schema and resolves any JSON references, including references in
128+ * externally-referenced files.
129+ *
130+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
131+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed and resolved
132+ * @param {function } [callback]
133+ * - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
134+ *
135+ * @returns {Promise }
136+ * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
137+ */
98138$RefParser . resolve = function ( schema , options , callback ) {
99139 return new $RefParser ( ) . resolve ( schema , options , callback ) ;
100140} ;
101141
142+ /**
143+ * Parses the given JSON schema and resolves any JSON references, including references in
144+ * externally-referenced files.
145+ *
146+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
147+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed and resolved
148+ * @param {function } [callback]
149+ * - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
150+ *
151+ * @returns {Promise }
152+ * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
153+ */
102154$RefParser . prototype . resolve = function ( schema , options , callback ) {
103155 if ( _isFunction ( options ) ) {
104156 callback = options ;
@@ -122,10 +174,28 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
122174 } ) ;
123175} ;
124176
177+ /**
178+ * Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
179+ * That is, all JSON references are replaced with their resolved values.
180+ *
181+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
182+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed, resolved, and dereferenced
183+ * @param {function } [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
184+ * @returns {Promise } - The returned promise resolves with the dereferenced JSON schema object.
185+ */
125186$RefParser . dereference = function ( schema , options , callback ) {
126187 return new $RefParser ( ) . dereference ( schema , options , callback ) ;
127188} ;
128189
190+ /**
191+ * Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
192+ * That is, all JSON references are replaced with their resolved values.
193+ *
194+ * @param {string|object } schema - The file path or URL of the JSON schema. Or a JSON schema object.
195+ * @param {ParserOptions } [options] - Options that determine how the schema is parsed, resolved, and dereferenced
196+ * @param {function } [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
197+ * @returns {Promise } - The returned promise resolves with the dereferenced JSON schema object.
198+ */
129199$RefParser . prototype . dereference = function ( schema , options , callback ) {
130200 if ( _isFunction ( options ) ) {
131201 callback = options ;
0 commit comments