Skip to content

IfThen with constant #61

@Eomm

Description

@Eomm

💬 Questions and Help

I don't find if it is possible to write this json schema:

{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": ["AS", "RP"]
    }
  },
  "if": {
    "properties": { "type": { "const": "RP" } }
  },
  "then": {
    "properties": { "issueType": { "type": "string" } },
    "required": [ "issueType" ]
  }
}

I'm trying the code:

const S = require('fluent-schema')

const fs = S.object()
  .prop('type', S.string().enum(['AS', 'CS', 'RP']))
  .ifThen(
    // S.object().prop('type', S.string().default('RP')),
    S.object().prop('type', 'RP'),
    S.object().prop('issueType', S.string().required())
  )

console.log(JSON.stringify(fs.valueOf(), null, 2))

But I get:

  "if": {
    "properties": {
      "type": {
        "0": "R",
        "1": "P"
      }
    }
  },

or

  "if": {
    "properties": {
      "type": {
        "type": "string",
        "default": "RP"
      }
    }
  },

Should I use S.raw?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions