question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement if/then/else syntax for conditional fields

See original GitHub issue

Prerequisites

  • I have read the documentation;
  • In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

The dependencies syntax is very powerful, however limitations are very quickly hit when dealing with fields across different objects. A usecase we have internally is: when: entity.legal_status != ‘limited_company’ then: ask for applicant.address

As additional incentive, there is a warning in the spec that dependencies could disappear.

Expected behavior

based on the example in the readme:

This can be written as chained if/then/else statements:

{
  "if": {"properties": {"Do you have any pets?": {"enum": ["No"]}}},
  "else": {
    "if": {"properties": {"Do you have any pets?": {"enum": ["Yes: One"]}}},
    "then": {
      "properties": {"How old is your pet?": {"type": "number"}},
      "required": ["How old is your pet?"]
    },
    "else": {
      "...": "etc"
    }
  }
}

or using a oneOf:

{
  "oneOf": [
    {
      "if": {"properties": {"Do you have any pets?": {"enum": ["No"]}}},
      "else": false
    },
    {
      "if": {"properties": {"Do you have any pets?": {"enum": ["Yes: One"]}}},
      "then": {
        "properties": {"How old is your pet?": {"type": "number"}},
        "required": ["How old is your pet?"]
      },
      "else": false
    },
    "..."
  ]
}

the false on else is to force the validity to evaluate falsely, so oneOf moves on

While implementing this, I came across a potential issue, in that utils.retrieveSchema doesn’t do a deep merge when resolving $refs. Given that the if/then/else syntax opens up the possibilities of doing cross-object conditioning, is this something that might need fixing? The main issue caused was that lists would not get appended, meaning if a $ref defined required, it would get discarded in favour of applied within a conditional.

If this is functionality people want here, I can prep a pull request.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:13
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ultroxcommented, Feb 28, 2018

@cybojenix, sorry for intrusion, but is there any way you can share your fork, or just that part of the code where you implemented that.

0reactions
jay-hodgsoncommented, Mar 19, 2020

PR #1610 is currently there as an implementation of this syntax.

@epicfaace : It looks like that PR has merge conflicts. Do those need to be resolved before consideration? (I don’t work in this project, so I apologize if this question is not applicable).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic conditional branching with if-then statements
To create a branch in our program, is to create an alternative sequence of commands that may be ignored, based on certain conditions...
Read more >
How to insert a conditional statement (If...Then...Else) in Word ...
You can use conditional statements to indicate what will display in a ... In the Insert this text field, enter the statement to...
Read more >
How to use conditional field If - Then - Else | Mail Merge
Learn how to write an IF - Then - Else field for a Microsoft Word mail merge and when to use one. Use...
Read more >
Beyond IF THEN ELSE: Techniques for Conditional Execution ...
This is commonly done using the IF…THEN…ELSE syntax. ... The most common method for implementing conditional logic using SAS software, and probably the ......
Read more >
How to Use IF...THEN Logic in SQL Server | Tutorial by Chartio
This is most easily accomplished in all versions of SQL Server using the CASE statement, which acts as a logical IF...THEN...ELSE expression and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found