Skip to content

Commit

Permalink
feat: YAML 1.2 (#499)
Browse files Browse the repository at this point in the history
* feat: YAML 1.2

* docs(js): syntax error

* test: rename case
  • Loading branch information
P0lip authored Aug 28, 2019
1 parent 2a85491 commit 492ad42
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
8 changes: 4 additions & 4 deletions docs/guides/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ responses:

// create a new instance of spectral with all of the baked in rulesets
const spectral = new Spectral();
spectral.run(myOpenApiDocument).then(results => console.log(results);
spectral.run(myOpenApiDocument).then(results => console.log(results));
```

Please note that by default Spectral supports YAML 1.2 with merge keys extension.

This will run Spectral with no rules or functions, so it's not going to do anything. Find out how to add rules and functions below.

## Linting an Object
Expand Down Expand Up @@ -55,14 +57,12 @@ Spectral comes with some rulesets that are very specific to OpenAPI v2/v3, and t
```js
const { Spectral } = require('@stoplight/spectral');
const { parseWithPointers } = require("@stoplight/yaml");
const { oas3Functions } = require('@stoplight/spectral/dist/rulesets/oas3');

const myOpenApiDocument = {
// any parsed open api document
};

const spectral = new Spectral();
spectral.addFunctions(oas3Functions());
spectral.loadRuleset('spectral:oas3') // spectral:oas2 for OAS 2.0 aka Swagger
.then(() => spectral.run(myOpenApiDocument))
.then(results => {
Expand Down Expand Up @@ -345,4 +345,4 @@ expect(result).toEqual([
code: 'rule1',
}),
]);
```
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@stoplight/json-ref-resolver": "^2.1.2",
"@stoplight/path": "^1.1.0",
"@stoplight/types": "^11.0.0",
"@stoplight/yaml": "^3.0.0",
"@stoplight/yaml": "^3.1.0",
"abort-controller": "^3.0.0",
"ajv": "^6.7",
"ajv-oai": "^1.1.1",
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/__fixtures__/implicit-strings.oas3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%YAML 1.2
---
openapi: 3.0.0
info:
title: Random title
description: Random description
version: 0.0.1
contact:
email: random@random.com
paths: {}
servers:
- url: https://www.random.com
components:
schemas:
RandomRequest:
description: A random request
type: object
properties:
implicit_string_date:
description: Some YAML 1.2 implicit string.
type: string
example: 2012-10-12
another_implicit_string_date:
description: Some string.
type: string
example: x20121012
explicit_string_date:
description: Explicit string.
type: string
example: "2012-10-12"
24 changes: 24 additions & 0 deletions src/__tests__/parsing.jest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from '@stoplight/path';
import { parseWithPointers } from '@stoplight/yaml';
import * as fs from 'fs';

const implicitStringOAS3Document = path.join(__dirname, './__fixtures__/implicit-strings.oas3.yaml');

describe('Parsing', () => {
it('parses YAML scalar values according to YAML 1.2', () => {
const { data } = parseWithPointers(fs.readFileSync(implicitStringOAS3Document, 'utf8'));

expect(data).toHaveProperty(
'components.schemas.RandomRequest.properties.implicit_string_date.example',
'2012-10-12',
);
expect(data).toHaveProperty(
'components.schemas.RandomRequest.properties.another_implicit_string_date.example',
'x20121012',
);
expect(data).toHaveProperty(
'components.schemas.RandomRequest.properties.explicit_string_date.example',
'2012-10-12',
);
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@
dependencies:
"@types/json-schema" "^7.0.3"

"@stoplight/yaml@^3.0.0":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@stoplight/yaml/-/yaml-3.0.2.tgz#acd567d580b35c7b2b8c9802a448ffcc70698507"
integrity sha512-VV4jQIYvRAR7nCnS7m6vPUrOcx0U6+47T/YmH77PdV56d6QJIsnHd7DusfnC5EDYBsW+deGnTjIPcRMlLmPE/g==
"@stoplight/yaml@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@stoplight/yaml/-/yaml-3.1.0.tgz#1026dd99512b987f6686e9daa52f67bd36ab4285"
integrity sha512-Db+f9AJO95S+bWL4ODdItLNXaBE/ey5nYAB9K/i9IgBZgJ+lyQ1+L0HSduX627uzwZCqgMo0tySWrgVTi2scBQ==
dependencies:
"@stoplight/types" "^11.0.0"
"@types/js-yaml" "3.12.1"
Expand Down

0 comments on commit 492ad42

Please sign in to comment.