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.

[QUESTION] Conditional validation in Model

See original GitHub issue

Hello!

Does Ts.ED provide a way to perform a conditional validation of a model based on another field in the model? Consider the following example

export enum Buyer {
  Person = "PERSON",
  Company = "COMPANY"
}

export class Invoice {
  @Required()
  @Enum(Buyer)
  buyer: Buyer;

  @Optional()
  // I'd like this field to be required only if buyer === 'COMPANY'
  companyData?: CompanyData

  @Optional()
  // ... and this field should be required only if buyer === 'PERSON'
  // something like:
  // @RequiredIf(o => o.buyer === 'PERSON')
  personData?: PersonData
}

For the record, it doesn’t really matter for me if this requirement is reflected in JSON Schema (both fields can be optional), I’m only caring about validation of request.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
securityMBcommented, Mar 16, 2021

I decided to just extend JsonSchemaObject like this:

declare module "@tsed/schema" {
  interface JsonSchemaObject {
    if?: Partial<JsonSchemaObject>;
    then?: Partial<JsonSchemaObject>;
    else?: Partial<JsonSchemaObject>;
  }
}

No more complains 😃

0reactions
Romakitacommented, Apr 7, 2021

I close this issue. This feature will be implemented with #1336

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET MVC Conditional validation - Stack Overflow
I would like to be the "Senior. Description" property conditional required field based on the selection of the "IsSenior" propery (true -> ...
Read more >
Conditional Validation -- Help Manual - Survey2Connect
Conditional Validation logic helps you validate the data that you have enter in a textbox question by creating an equation.
Read more >
DotVVM - Build Conditional Validation Attribute - C# Corner
You can validate your view model by using validation attributes. DotVVM does client side validation for only the following attributes:.
Read more >
RequireIf conditional validation in MVC - MSDN - Microsoft
User-730945238 posted. In my model I have the below property. public int? Data { get; set; } and using `@Html.TextBoxFor(m => m.
Read more >
Conditional ASP.NET MVC Validation using Data Annotation ...
In projects, there will be cases when you need to have conditional validation depending on the action method or even the data.
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