Skip to content

how to handle undefined values cleanly? #571

Closed
@dcsan

Description

@dcsan

I'm dump ing an object, and fields without values come out like this:

fieldName: !<tag:yaml.org,2002:js/undefined> ''

how can I suppress that to either undefined or just to skip it like JSON.stringify ?

I tried some options from the safeDump

const blob = yaml.dump(obj, { skipInvalid: true, lineWidth: 200 })

https://github.com/nodeca/js-yaml#safedump-object---options-

but without success. I guess I can strip out the undefined fields, but I'd have to do a deep clean of the whole object... also check for false vs undefined etc. etc.

I tried this but it didn't have any effect:
const clean = { ...obj } // remove nulls?

Sure there must be a nicer way to handle this with js-yaml ?
Do I have to define my own schema?

related:
#456
https://stackoverflow.com/a/38340374/1146785

  async debugMessage(obj) {
    console.log('json', JSON.stringify(obj, null, 2))
    const blob = yaml.dump(obj)
    console.log('yaml', blob)
  }


// json 

{
  "msg": "router",
  "input": "sleep",
  "eventType": "action",
  "handled": {
    "handled": true,
    "doc": {
      "match": "^cont|.*",
      "goto": "prologue"
    },
    "klass": "room",
    "history": [
      "goto"
    ]
  }
}

// yaml 

msg: router
input: sleep
eventType: action
parsed: !<tag:yaml.org,2002:js/undefined> ''
handled:
  handled: true
  doc:
    match: ^cont|.*
    goto: prologue
  klass: room
  history:
    - goto

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions