Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ $RefParser.dereference("my-schema.yaml", {
-------------------
The `parse` options determine how different types of files will be parsed.

JSON Schema $Ref Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add [your own custom parsers](plugins/parsers.md) if you want.

|Option(s) |Type |Description
|:----------------------------|:----------|:------------
|`json`<br>`yaml`<br>`text`<br>`binary`|`object` `boolean`|These are the built-in parsers. In addition, you can add [your own custom parsers](plugins/parsers.md)<br><br>To disable a parser, just set it to `false`.
|`json.order` `yaml.order` `text.order` `binary.order`|`number`|Parsers run in a specific order, relative to other parsers. For example, a parser with `order: 5` will run _before_ a parser with `order: 10`. If a parser is unable to successfully parse a file, then the next parser is tried, until one succeeds or they all fail.<br><br>You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add [your own custom parser](plugins/parsers.md) that you want to run _first_.
|`json.allowEmpty` `yaml.allowEmpty` `text.allowEmpty` `binary.allowEmpty`|`boolean`|All of the built-in parsers allow empty files by default. The JSON and YAML parsers will parse empty files as `undefined`. The text parser will parse empty files as an empty string. The binary parser will parse empty files as an empty byte array.<br><br>You can set `allowEmpty: false` on any parser, which will cause an error to be thrown if a file empty.
|`json.canParse` `yaml.canParse` `text.canParse` `binary.canParse`|`boolean`, `RegExp`, `string`, `array`, `function`|Determines which parsers will be used for which files.<br><br>A regular expression can be used to match files by their full path. A string (or array of strings) can be used to match files by their file extension. Or a function can be used to perform more complex matching logic. See the [custom parser](plugins/parsers.md) docs for details.
|`text.encoding`|`string` |The encoding to use when parsing text-based files. The default is "utf8".
JSON Schema $Ref Parser comes with built-in JSON, YAML, and plain-text parsers, any of which you can configure or disable. You can also add [your own custom parsers](plugins/parsers.md) if you want.

| Option(s) |Type |Description
|:-----------------------------------------------------------------------|:----------|:------------
| `json`<br>`yaml`<br>`text` |`object` `boolean`|These are the built-in parsers. In addition, you can add [your own custom parsers](plugins/parsers.md)<br><br>To disable a parser, just set it to `false`.
| `json.order` `yaml.order` `text.order` |`number`|Parsers run in a specific order, relative to other parsers. For example, a parser with `order: 5` will run _before_ a parser with `order: 10`. If a parser is unable to successfully parse a file, then the next parser is tried, until one succeeds or they all fail.<br><br>You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add [your own custom parser](plugins/parsers.md) that you want to run _first_.
| `json.allowEmpty` `yaml.allowEmpty` `text.allowEmpty` |`boolean`|All of the built-in parsers allow empty files by default. The JSON and YAML parsers will parse empty files as `undefined`. The text parser will parse empty files as an empty string.
| `json.canParse` `yaml.canParse` `text.canParse` |`boolean`, `RegExp`, `string`, `array`, `function`|Determines which parsers will be used for which files.<br><br>A regular expression can be used to match files by their full path. A string (or array of strings) can be used to match files by their file extension. Or a function can be used to perform more complex matching logic. See the [custom parser](plugins/parsers.md) docs for details.
| `text.encoding` |`string` |The encoding to use when parsing text-based files. The default is "utf8".


`resolve` Options
Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/parsers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Custom Parsers
==========================

JSON Schema $Ref Parser comes with built-in JSON, YAML, plain-text, and binary parsers, but you can add your own parsers to support additional file types, or even replace any of the built-in parsers with your own custom implementation.
JSON Schema $Ref Parser comes with built-in JSON, YAML, and plain-text parsers, but you can add your own parsers to support additional file types, or even replace any of the built-in parsers with your own custom implementation.

You can see the source code for any of the built-in parsers [right here](../../lib/parsers).

Expand All @@ -26,7 +26,7 @@ $RefParser.dereference(mySchema, { parse: { csv: myParser }});
```

#### The `order` property
All parsers have an `order` property, even the built-in parsers. If you don't specify an `order` property, then your parser will run last. Specifying `order: 1`, like we did in this example, will make your parser run first. Or you can squeeze your parser in-between some of the built-in parsers. For example, `order: 201` would make it run _after_ the JSON and YAML parsers, but _before_ the plain-text and binary parsers. You can see the order of all the built-in parsers by looking at [their source code](../../lib/parsers).
All parsers have an `order` property, even the built-in parsers. If you don't specify an `order` property, then your parser will run last. Specifying `order: 1`, like we did in this example, will make your parser run first. Or you can squeeze your parser in-between some of the built-in parsers. For example, `order: 201` would make it run _after_ the JSON and YAML parsers, but _before_ the plain-text parser. You can see the order of all the built-in parsers by looking at [their source code](../../lib/parsers).

The `order` property and `canParse` property are related to each other. For each file that JSON Schema $Ref Parser needs to parse, it first determines which parsers _can_ parse that file by checking their `canParse` property. If only one parser matches a file, then _only_ that one parser is called, regardless of its `order`. If multiple parsers match a file, then those parsers are tried _in order_ until one of them successfully parses the file. Once a parser successfully parses the file, the rest of the parsers are skipped.

Expand Down
2 changes: 0 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const jsonParser = require("./parsers/json");
const yamlParser = require("./parsers/yaml");
const textParser = require("./parsers/text");
const binaryParser = require("./parsers/binary");
const fileResolver = require("./resolvers/file");
const httpResolver = require("./resolvers/http");

Expand Down Expand Up @@ -32,7 +31,6 @@ $RefParserOptions.defaults = {
json: jsonParser,
yaml: yamlParser,
text: textParser,
binary: binaryParser,
},

/**
Expand Down
55 changes: 0 additions & 55 deletions lib/parsers/binary.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/specs/blank/blank.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,17 @@ describe("Blank files", () => {
path.abs("specs/blank/files/blank.yaml"), parsedSchema.yaml,
path.abs("specs/blank/files/blank.json"), parsedSchema.json,
path.abs("specs/blank/files/blank.txt"), parsedSchema.text,
path.abs("specs/blank/files/blank.png"), parsedSchema.binary,
path.abs("specs/blank/files/blank.foo"), parsedSchema.unknown
));

it("should dereference successfully", async () => {
let schema = await $RefParser.dereference(path.rel("specs/blank/blank.yaml"));
schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary);
expect(schema).to.deep.equal(dereferencedSchema);
});

it("should bundle successfully", async () => {
let schema = await $RefParser.bundle(path.rel("specs/blank/blank.yaml"));
schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary);
expect(schema).to.deep.equal(dereferencedSchema);
});

it('should throw an error if "allowEmpty" is disabled', async () => {
try {
await $RefParser.dereference(path.rel("specs/blank/blank.yaml"), { parse: { binary: { allowEmpty: false }}});
helper.shouldNotGetCalled();
}
catch (err) {
expect(err).to.be.an.instanceOf(SyntaxError);
expect(err.message).to.contain("Error parsing ");
expect(err.message).to.contain("blank/files/blank.png");
expect(err.message).to.contain("Parsed value is empty");
}
});
});
});
2 changes: 0 additions & 2 deletions test/specs/blank/blank.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ yaml:
$ref: files/blank.yaml
text:
$ref: files/blank.txt
binary:
$ref: files/blank.png
unknown:
$ref: files/blank.foo
1 change: 0 additions & 1 deletion test/specs/blank/dereferenced.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ module.exports = {
json: undefined,
yaml: undefined,
text: "",
binary: { type: "Buffer", data: []},
unknown: undefined
};
Empty file removed test/specs/blank/files/blank.png
Empty file.
5 changes: 0 additions & 5 deletions test/specs/blank/parsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ module.exports =
text: {
$ref: "files/blank.txt"
},
binary: {
$ref: "files/blank.png"
},
unknown: {
$ref: "files/blank.foo"
},
Expand All @@ -26,7 +23,5 @@ module.exports =

text: "",

binary: { type: "Buffer", data: []},

unknown: undefined
};
Loading