Skip to content

Commit

Permalink
feat: parse scalar values according to YAML 1.2 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Aug 28, 2019
1 parent 245b40f commit f90b45e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/__tests__/fixtures/spectral-481.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: [email protected]
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"
17 changes: 17 additions & 0 deletions src/__tests__/parseWithPointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HugeYAML } from './fixtures/huge-yaml';

const diverse = fs.readFileSync(path.join(__dirname, './fixtures/diverse.yaml'), 'utf-8');
const duplicateMergeKeys = fs.readFileSync(path.join(__dirname, './fixtures/duplicate-merge-keys.yaml'), 'utf-8');
const spectral481 = fs.readFileSync(path.join(__dirname, './fixtures/spectral-481.yaml'), 'utf-8');

describe('yaml parser', () => {
test.each(['test', 1])('parse scalar $s', val => {
Expand All @@ -29,6 +30,22 @@ describe('yaml parser', () => {
expect(result.data).toEqual(HugeJSON);
});

it('parses string according to YAML 1.2 spec', () => {
const { data } = parseWithPointers(spectral481);
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'
);
});

describe('report errors', () => {
test('unknown tags', () => {
const result = parseWithPointers(`function: !!js/function >
Expand Down
23 changes: 22 additions & 1 deletion src/parseWithPointers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { DiagnosticSeverity, IDiagnostic } from '@stoplight/types';
import {
determineScalarType,
Kind,
load as loadAST,
parseYamlBoolean,
parseYamlFloat,
parseYamlInteger,
ScalarType,
YAMLAnchorReference,
YAMLException,
YamlMap,
YAMLNode,
YAMLScalar,
YAMLSequence,
} from 'yaml-ast-parser';
import { buildJsonPath } from './buildJsonPath';
Expand Down Expand Up @@ -92,7 +98,7 @@ export const walkAST = (
case Kind.SEQ:
return (node as YAMLSequence).items.map(item => walkAST(item, options, duplicatedMappingKeys));
case Kind.SCALAR:
return 'valueObject' in node ? node.valueObject : node.value;
return getScalarValue(node as YAMLScalar);
case Kind.ANCHOR_REF:
if (node.value !== void 0 && isCircularAnchorRef(node as YAMLAnchorReference)) {
node.value = dereferenceAnchor(node.value, (node as YAMLAnchorReference).referencesAnchor);
Expand Down Expand Up @@ -150,6 +156,21 @@ const dereferenceAnchor = (node: YAMLNode, anchorId: string): YAMLNode | YAMLNod
}
};

function getScalarValue(node: YAMLScalar): number | null | boolean | string | void {
switch (determineScalarType(node)) {
case ScalarType.null:
return null;
case ScalarType.string:
return String(node.value);
case ScalarType.bool:
return parseYamlBoolean(node.value);
case ScalarType.int:
return parseYamlInteger(node.value);
case ScalarType.float:
return parseYamlFloat(node.value);
}
}

// builds up the line map, for use by linesForPosition
const computeLineMap = (input: string) => {
const lineMap: number[] = [];
Expand Down

0 comments on commit f90b45e

Please sign in to comment.