Parse Only true/false as Booleans, Keep True/TRUE as Strings in [email protected] #635
fuyalasmit
started this conversation in
General
Replies: 1 comment 7 replies
-
Here's how I would customize the core schema to provide what you want: import { Schema, parse } from 'yaml'
const schema = new Schema({ schema: 'core' })
for (let i = 0; i < schema.tags.length; ++i)
if (schema.tags[i].tag === 'tag:yaml.org,2002:bool')
schema.tags[i] = { ...schema.tags[i], test: /^(?:true|false)$/ }
parse('[ true, True, TRUE ]', { schema })
// [ true, 'True', 'TRUE' ] It looks like the ability to use a Schema instance as the |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How to parse only lowercase true/false as booleans, preserve True/TRUE/False/FALSE as strings?
I'm working on a project where I need to parse YAML such that only lowercase
true
andfalse
are treated as booleans, while values likeTrue
,TRUE
,False
,FALSE
are parsed as strings to preserve their exact capitalization in the JSON output.Example YAML:
Desired JSON output:
I've tried using
schema: 'json'
but it's too restrictive and causes parsing errors for other valid YAML constructs. Is there a way to create a custom schema or use parsing options that only treats lowercase true/false as booleans?Any help would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions