diff --git a/docs/guides/javascript.md b/docs/guides/javascript.md index 5e3ef1f55..0dbf49b6e 100644 --- a/docs/guides/javascript.md +++ b/docs/guides/javascript.md @@ -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 @@ -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 => { @@ -345,4 +345,4 @@ expect(result).toEqual([ code: 'rule1', }), ]); -``` \ No newline at end of file +``` diff --git a/package.json b/package.json index 23482ac09..26ac472b1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/__fixtures__/implicit-strings.oas3.yaml b/src/__tests__/__fixtures__/implicit-strings.oas3.yaml new file mode 100644 index 000000000..032699dc4 --- /dev/null +++ b/src/__tests__/__fixtures__/implicit-strings.oas3.yaml @@ -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" diff --git a/src/__tests__/parsing.jest.test.ts b/src/__tests__/parsing.jest.test.ts new file mode 100644 index 000000000..c13da33ec --- /dev/null +++ b/src/__tests__/parsing.jest.test.ts @@ -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', + ); + }); +}); diff --git a/yarn.lock b/yarn.lock index 04078d16f..9dfe87aa1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"