Skip to content

Commit 5318f11

Browse files
Exposed YAML.parse() and YAML.stringify()
1 parent 90cbe3f commit 5318f11

8 files changed

Lines changed: 469 additions & 703 deletions

File tree

README.md

Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The API
130130
- [`dereference()`](#dereferencepath-options-callback)
131131
- [`validate()`](#validatepath-options-callback)
132132
- Objects
133+
- [`Options`](#options)
133134
- [`Schema`](#schema-object)
134135
- [`$Refs`](#refs-object)
135136
- [`$Refs.paths()`](#refspathstypes)
@@ -139,7 +140,9 @@ The API
139140
- [`$Refs.exists()`](#refsexistsref)
140141
- [`$Refs.get()`](#refsgetref-options)
141142
- [`$Refs.set()`](#refssetref-value-options)
142-
- [`Options`](#options)
143+
- [`YAML`](#yaml-object)
144+
- [`YAML.parse()`](#yamlparsetext)
145+
- [`YAML.stringify()`](#yamlstringifyvalue)
143146
- [Class methods vs. Instance methods](#class-methods-vs-instance-methods)
144147
- [Callbacks vs. Promises](#callbacks-vs-promises)
145148

@@ -258,6 +261,38 @@ $RefParser.dereference("my-schema.yaml")
258261
```
259262

260263

264+
### Options
265+
You can pass an options parameter to any method. You don't need to specify every option - only the ones you want to change.
266+
267+
```javascript
268+
$RefParser.dereference("my-schema.yaml", {
269+
allow: {
270+
json: false, // Don't allow JSON files
271+
yaml: true // Allow YAML files
272+
},
273+
$refs: {
274+
internal: false // Don't dereference internal $refs, only external
275+
},
276+
cache: {
277+
fs: 1, // Cache local files for 1 second
278+
http: 600 // Cache http URLs for 10 minutes
279+
}
280+
});
281+
```
282+
283+
|Option |Type |Default |Description
284+
|:----------------|:--------|:---------|:----------
285+
|`allow.json` |bool |true |Determines whether JSON files are supported
286+
|`allow.yaml` |bool |true |Determines whether YAML files are supported<br> (note: all JSON files are also valid YAML files)
287+
|`allow.empty` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an empty file
288+
|`allow.unknown` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an unknown/unsupported file type (such as HTML, text, image, etc.). The default is to resolve unknown files as a [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer)
289+
|`$refs.internal` |bool |true |Determines whether internal `$ref` pointers (such as `#/definitions/widget`) will be dereferenced when calling [`dereference()`](#dereferencepath-options-callback). Either way, you'll still be able to get the value using [`$refs.get()`](#refsgetref-options)
290+
|`$refs.external` |bool |true |Determines whether external `$ref` pointers get resolved/dereferenced. If `false`, then no files/URLs will be retrieved. Use this if you only want to allow single-file schemas.
291+
|`cache.fs` |number |60 |<a name="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
292+
|`cache.http` |number |300 |The length of time (in seconds) to cache HTTP URLs. The default is five minutes. Setting to zero will cache forever.
293+
|`cache.https` |number |300 |The length of time (in seconds) to cache HTTPS URLs. The default is five minutes. Setting to zero will cache forever.
294+
295+
261296
### `Schema` Object
262297
If you create an instance of the `$RefParser` class (rather than just calling the static methods), then the `schema` property gives you easy access to the JSON schema. This is the same value that is passed to the callback function (or Promise) when calling the [`parse`](#parsepath-options-callback), [`bundle`](#bundlepath-options-callback), or [`dereference`](#dereferencepath-options-callback) methods.
263298

@@ -422,36 +457,88 @@ $RefParser.resolve("my-schema.json")
422457
```
423458

424459

425-
### Options
426-
You can pass an options parameter to any method. You don't need to specify every option - only the ones you want to change.
460+
### `YAML` object
461+
This object provides simple YAML parsing functions. JSON Schema $Ref Parser uses this object internally
462+
for its own YAML parsing, but it is also exposed so you can use it in your code if needed.
463+
464+
465+
### `YAML.parse(text)`
466+
467+
- **text** (_required_) - `string`<br>
468+
The YAML string to be parsed.
469+
470+
- **Return Value:**<br>
471+
Returns the parsed value, which can be any valid JSON type (object, array, string, number, etc.)
472+
473+
This method is similar to [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse), but it supports YAML _in addition_ to JSON (since any JSON document is also a valid YAML document).
427474

428475
```javascript
429-
$RefParser.dereference("my-schema.yaml", {
430-
allow: {
431-
json: false, // Don't allow JSON files
432-
yaml: true // Allow YAML files
433-
},
434-
$refs: {
435-
internal: false // Don't dereference internal $refs, only external
476+
var YAML = $RefParser.YAML;
477+
var text = "title: person \n" +
478+
"required: \n" +
479+
" - name \n" +
480+
" - age \n" +
481+
"properties: \n" +
482+
" name: \n" +
483+
" type: string \n" +
484+
" age: \n" +
485+
" type: number"
486+
487+
var obj = YAML.parse(text);
488+
489+
// {
490+
// title: "person",
491+
// required: ["name", "age"],
492+
// properties: {
493+
// name: {
494+
// type: "string"
495+
// },
496+
// age: {
497+
// type: "number"
498+
// }
499+
// }
500+
// }
501+
```
502+
503+
504+
### `YAML.stringify(value)`
505+
506+
- **value** (_required_)<br>
507+
The value to be converted to a YAML string. Can be any valid JSON type (object, array, string, number, etc.)
508+
509+
- **Return Value:** `string`<br>
510+
Returns the a YAML string containing the serialized value
511+
512+
This method is similar to [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify), except that it converts a value to a YAML string instead of a JSON string.
513+
514+
```javascript
515+
var YAML = $RefParser.YAML;
516+
var obj = {
517+
title: "person",
518+
required: ["name", "age"],
519+
properties: {
520+
name: {
521+
type: "string"
436522
},
437-
cache: {
438-
fs: 1, // Cache local files for 1 second
439-
http: 600 // Cache http URLs for 10 minutes
523+
age: {
524+
type: "number"
440525
}
441-
});
442-
```
526+
}
527+
};
443528

444-
|Option |Type |Default |Description
445-
|:----------------|:--------|:---------|:----------
446-
|`allow.json` |bool |true |Determines whether JSON files are supported
447-
|`allow.yaml` |bool |true |Determines whether YAML files are supported<br> (note: all JSON files are also valid YAML files)
448-
|`allow.empty` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an empty file
449-
|`allow.unknown` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an unknown/unsupported file type (such as HTML, text, image, etc.). The default is to resolve unknown files as a [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer)
450-
|`$refs.internal` |bool |true |Determines whether internal `$ref` pointers (such as `#/definitions/widget`) will be dereferenced when calling [`dereference()`](#dereferencepath-options-callback). Either way, you'll still be able to get the value using [`$refs.get()`](#refsgetref-options)
451-
|`$refs.external` |bool |true |Determines whether external `$ref` pointers get resolved/dereferenced. If `false`, then no files/URLs will be retrieved. Use this if you only want to allow single-file schemas.
452-
|`cache.fs` |number |60 |<a name="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
453-
|`cache.http` |number |300 |The length of time (in seconds) to cache HTTP URLs. The default is five minutes. Setting to zero will cache forever.
454-
|`cache.https` |number |300 |The length of time (in seconds) to cache HTTPS URLs. The default is five minutes. Setting to zero will cache forever.
529+
530+
var string = YAML.stringify(obj);
531+
532+
// title: person
533+
// required:
534+
// - name
535+
// - age
536+
// properties:
537+
// name:
538+
// type: string
539+
// age:
540+
// type: number
541+
```
455542

456543

457544
### Class methods vs. Instance methods

0 commit comments

Comments
 (0)