You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function.
52
52
53
53
54
54
### Callbacks vs. Promises
55
-
Many people prefer [ES6 Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
55
+
Many people prefer [Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) or `async`/`awiat` instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
56
56
57
-
If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return an ES6 Promise.
57
+
If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return a Promise.
Copy file name to clipboardExpand all lines: docs/plugins/parsers.md
+41-36Lines changed: 41 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,14 @@ You can see the source code for any of the built-in parsers [right here](../../l
9
9
The fastest way to learn is by example, so let's do that. Here's a simplistic parser that parses CSV files (comma-separated values):
10
10
11
11
```javascript
12
-
var myParser = {
12
+
let myParser = {
13
13
order:1,
14
14
15
15
canParse:".csv",
16
16
17
-
parse:function(file) {
18
-
var lines =file.data.toString().split("\n");
19
-
returnlines.map(function(line) {
17
+
parse(file) {
18
+
let lines =file.data.toString().split("\n");
19
+
returnlines.map((line)=> {
20
20
returnline.split(",");
21
21
});
22
22
}
@@ -37,55 +37,60 @@ If _none_ of the parsers match the file, then _all_ of them are tried, in order,
37
37
The `canParse` property tells JSON Schema $Ref Parser what kind of files your parser can handle. In this example, we've simply specified a file extension, but we could have used a simple boolean, an array of file extensions, a regular expression, or even a function with custom logic to determine which files to parse. Here are examples of each approach:
38
38
39
39
```javascript
40
-
// Parse ALL file types
41
-
canParse:true
40
+
let myParser = {
41
+
// Parse ALL file types
42
+
canParse:true
42
43
43
-
// An array of file extensions (lowercased)
44
-
canParse: [".txt", ".csv"]
44
+
// An array of file extensions (lowercased)
45
+
canParse: [".txt", ".csv"]
45
46
46
-
// A regular expression (matched against the FULL file path)
47
-
canParse:/\.(txt|csv)$/i
47
+
// A regular expression (matched against the FULL file path)
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being parsed.
56
58
57
59
#### The `parse` method
58
60
Obviously, this is where the real work of a parser happens. The `parse` method accepts the same [file info object](file-info-object.md) as the `canParse` function, but rather than returning a boolean value, the `parse` method should return a JavaScript representation of the file contents. For our CSV parser, that is a two-dimensional array of lines and values. For your parser, it might be an object, a string, a custom class, or anything else.
59
61
60
-
Unlike the `canParse` function, the `parse` method can also be asynchronous. This might be important if your parser needs to retrieve data from a database or if it relies on an external HTTP service to return the parsed value. You can return your asynchronous value using either an [ES6 Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Here are examples of both approaches:
62
+
Unlike the `canParse` function, the `parse` method can also be asynchronous. This might be important if your parser needs to retrieve data from a database or if it relies on an external HTTP service to return the parsed value. You can return your asynchronous value via a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Here are examples of both approaches:
Copy file name to clipboardExpand all lines: docs/plugins/resolvers.md
+43-38Lines changed: 43 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,18 +9,18 @@ You can see the source code for any of the built-in resolvers [right here](../..
9
9
The fastest way to learn is by example, so let's do that. Here's a simplistic resolver that reads data from a MongoDB database:
10
10
11
11
```javascript
12
-
var myResolver = {
12
+
let myResolver = {
13
13
order:1,
14
14
15
15
canRead:/^mongodb:/i,
16
16
17
-
read:function(file, callback) {
18
-
MongoClient.connect(file.url, function(err, db) {
17
+
read(file, callback) {
18
+
MongoClient.connect(file.url, (err, db)=> {
19
19
if (err) {
20
20
callback(err);
21
21
}
22
22
else {
23
-
db.find({}).toArray(function(err, document) {
23
+
db.find({}).toArray((err, document)=> {
24
24
callback(null, document);
25
25
});
26
26
}
@@ -41,16 +41,18 @@ The `order` property and `canRead` property are related to each other. For each
41
41
The `canRead` property tells JSON Schema $Ref Parser what kind of files your resolver can read. In this example, we've simply specified a regular expression that matches "mogodb://" URLs, but we could have used a simple boolean, or even a function with custom logic to determine which files to resolve. Here are examples of each approach:
42
42
43
43
```javascript
44
-
// Read ALL files
45
-
canRead:true
44
+
let myResolver = {
45
+
// Read ALL files
46
+
canRead:true
46
47
47
-
// Read all files on localhost
48
-
canRead:/localhost/i
48
+
// Read all files on localhost
49
+
canRead:/localhost/i
49
50
50
-
// A function that returns a truthy/falsy value
51
-
canRead:function(file) {
52
-
returnfile.url.indexOf("127.0.0.1") !==-1;
53
-
}
51
+
// A function that returns a truthy/falsy value
52
+
canRead(file) {
53
+
returnfile.url.indexOf("127.0.0.1") !==-1;
54
+
}
55
+
};
54
56
```
55
57
56
58
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being resolved.
@@ -59,40 +61,43 @@ When using the function form, the `file` parameter is a [file info object](file-
59
61
#### The `read` method
60
62
This is where the real work of a resolver happens. The `read` method accepts the same [file info object](file-info-object.md) as the `canRead` function, but rather than returning a boolean value, the `read` method should return the contents of the file. The file contents should be returned in as raw a form as possible, such as a string or a byte array. Any further parsing or processing should be done by [parsers](parsers.md).
61
63
62
-
Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value using either an [ES6 Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
64
+
Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value via a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
0 commit comments