Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compat option #335

Merged
merged 3 commits into from
Nov 26, 2021
Merged

Add compat option #335

merged 3 commits into from
Nov 26, 2021

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Nov 13, 2021

Fixes #329

Add a compatibility checker to the schema. The API is a bit different from that proposed in the issue:

export type SchemaOptions = {
  /**
   * When parsing, warn about compatibility issues with the given schema.
   * When stringifying, use scalar styles that are parsed correctly
   * by the `compat` schema as well as the actual schema.
   *
   * Default: `null`
   */
  compat?: string | Tags | null,
  ...
}

This is how it works:

import { parse } from 'yaml'

parse('x: true\ny: no', { compat: 'yaml-1.1' })
> { x: true, y: 'no' }
[TAG_RESOLVE_FAILED] YAMLWarning: Value may be parsed as either !!str or !!bool at line 2, column 1:

x: true
y: no
^
[TAG_RESOLVE_FAILED] YAMLWarning: Value may be parsed as either !!str or !!bool at line 2, column 4:

y: no
   ^^

This option serves at least these two different audiences:

  • When parsing, it may be used e.g. in a linter or other validator to note values that may be parsed differently by different libraries.
  • When stringifying, it may be used to output YAML that has a very high likelihood of being parsed as intended in libraries that use a different schema.

@eemeli eemeli linked an issue Nov 15, 2021 that may be closed by this pull request
@eemeli eemeli merged commit c6cc0c5 into master Nov 26, 2021
@eemeli eemeli deleted the compat-schema branch November 26, 2021 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a compatibility schema Incorrect error for flow sequence with flow mapping
1 participant