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.

conditional optional

See original GitHub issue

Is there any way to have a ‘conditional optional’? I’m trying to make a situation work where a key would only be required when another key is set.

new SimpleSchema({
  firstname: {
    type: String,
    label: "First name",
    optional: true
  },
  lastname: {
    type: String,
    label: "Last name",
    optional: true
  }
})

Considering this schema. How would I go ahead and make lastname required or optional: false when and only when firstname is set?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
andrenanningacommented, Mar 7, 2014

Hi Aldeed,

With the recent deprecation of valueIsAllowed I instead tried to use a custom method to do this conditional optional check. But this issue (#30) seems to have resurfaced.

Consider this schema:

MySchema = new SimpleSchema({
  firstname: {
    type: String,
    label: "First name",
    optional: true,
    custom: function() {
      if(/* condition where this field would be required */) {
        return 'required';
      }
    }
  }
}

In this example the custom method isn’t triggered when the firstname is empty.

Do you know how we could solve this?

Thanks.

0reactions
chrischecommented, Mar 28, 2014

No, sorry, what I said was not correct. They are not SimpleSchema instances, they are Collection2 instances. ( do you have a reference to where you do those basic tests ? I am not familiar with the code yet, but interested and also interested in having an implementation of foreign Keys. )

And the problem here was, that I didn’t check if the key was set, so I tried to validate something which was not updated. Now I check in my custom function if the key isSet and if so, I check for the value existing in the other collection. Works like a charm now 😃

Thanks for the help !

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the difference between Required, Conditional and ...
OPTIONAL – This indicates that the MSDS system cannot determine when or for which students the data are required. This does not mean...
Read more >
What is the difference between Required, Conditionally ...
Optional fields are not required to save the credit as complete and have no impact on scoring. They are included to give participants...
Read more >
Conditional optional checks - Using Swift
I have two optional properties and would like to know how to elegantly unwrap them to perform a conditional check.
Read more >
Mandatory/Conditional/Optional Fields - SmartBear Community
HI, I need help to create json schema with mandatory/conditional/optional field. For mandatory i am taking it inside "required".
Read more >
Conditionally optional/conditionally readonly properties #44261
the intent here is that getKey is optional if T is string or a subtype of string , but otherwise getKey is mandatory....
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