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

Dump adds quotes to some keys when using custom tag #659

Closed
m-tartari opened this issue Mar 11, 2022 · 3 comments
Closed

Dump adds quotes to some keys when using custom tag #659

m-tartari opened this issue Mar 11, 2022 · 3 comments

Comments

@m-tartari
Copy link

m-tartari commented Mar 11, 2022

As written in the title, yaml.dump adds quotes to some keys when using custom tags.

I've made a code sandbox (you can find here) and below a code sample where the issue is visible:

// sample.ts
import yaml from "js-yaml";

interface PoseType {
  x: number;
  y: number;
  z: number;
  rx: number;
  ry: number;
  rz: number;
  rw?: number;
}

class PoseTag {
  public pose: PoseType;
  constructor(props: PoseType) {
    this.pose = props;
  }
}

const PoseYamlType = new yaml.Type("!Pose", {
  kind: "mapping",
  resolve: (data) => {
    const val =
      typeof data.x === "number" &&
      typeof data.y === "number" &&
      typeof data.z === "number" &&
      typeof data.rx === "number" &&
      typeof data.ry === "number" &&
      typeof data.rz === "number" &&
      typeof data.rw === ("number" || "undefined");
    return val;
  },
  construct: (data: PoseType) => new PoseTag(data),
  instanceOf: PoseTag,
  represent: (obj: PoseTag) => obj.pose
});

// create schema
const schema = yaml.DEFAULT_SCHEMA.extend([PoseYamlType]);

const testPose: PoseType = {
  x: 2.46,
  y: 26.853,
  z: -9.792,
  rx: -0.969,
  ry: -0.001,
  rz: -0.233,
  rw: 0.078
};

const obj = new PoseTag(testPose);

const output = yaml.dump(obj, { schema: schema });
console.log(output);

expected output:

!Pose 
x: 2.46
y: 26.853
z: -9.792
rx: -0.969
ry: -0.001
rz: -0.233
rw: 0.078

received:

!Pose 
x: 2.46
'y': 26.853 # <---
z: -9.792
rx: -0.969
ry: -0.001
rz: -0.233
rw: 0.078
@rlidwka
Copy link
Member

rlidwka commented May 9, 2022

y without quotes is a boolean true value in YAML 1.0, old version of the spec: https://yaml.org/type/bool.html

so it's intentional, but we probably should drop support for that at some point

@rlidwka
Copy link
Member

rlidwka commented May 9, 2022

gonna close this, duplicate of #645

@rlidwka rlidwka closed this as completed May 9, 2022
@rlidwka
Copy link
Member

rlidwka commented May 9, 2022

you can use

yaml.dump(obj, { schema: schema, noCompatMode: true });

it may be subject to change (maybe true by default) in next major version

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

No branches or pull requests

2 participants