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: how to validate 2D arrays?

See original GitHub issue

Hello,

I have something like this

@Type is from class-transformer. I tried to search for the issues but I could not find anything.

export class Child {
  @IsDefined()
  @IsString()
  @IsNotEmpty()
  name: string;
}

export class Parent {
  @IsArray()
  @IsNotEmpty()
  @Type((type) => Child)
  @ValidateNested()
  children: Child[][];
}

const parent = new Parent();
let res;
// I need `as any` because I'm using strictNullChecks
parent.children= 123 as any; 
res = validateSync(parent); // this fails

parent.children= [] as any; 
res = validateSync(parent); // this passes

parent.children= [[]] as any; 
res = validateSync(parent); // this passes

parent.children= [[ { name: null } ]] as any;
res = validateSync(parent);  // this passes

Even if the type Child is changed to JS native types like number or string. How can I validate something like this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MarkMurphycommented, Jan 19, 2021

Currently we do not have support for multidimensional arrays. You have to create your own custom validator.

Not really sure how that would work, can you point us in the right direction here?

0reactions
NoNameProvidedcommented, Dec 9, 2022

As mentioned by others, you can use a custom validator for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validating elements of a 2D array - java - Stack Overflow
You have three problems here, two of which are critical. First, if you want to iterate over each subarray, test its length, not...
Read more >
Check for possible path in 2D matrix - GeeksforGeeks
Given a 2D array(m x n). The task is to check if there is any path from top left to bottom right. In...
Read more >
Validate if string is correctly formed 2D array - Sololearn
What I'd like to do atm is parse a user input string to check if it is a valid 2D array.
Read more >
2. Given an 2D array of n xn, write a function to | Chegg.com
Question : 2. Given an 2D array of n xn, write a function to validate whether that is a symmetric matrix or not....
Read more >
How do I do input validation for 2D array in C++? - Quora
It depends upon how the 2 dimensional array was constructed. You must follow the actual layout of the data to properly set the...
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