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.

feat: support multi-dimensional arrays

See original GitHub issue

Hello, I am trying to validate the arrays in arrays but I could not find a correct method.

The structure looks like this:

class MyInnerDto {
 @IsString()
  name: string;
}
 
class TestDto {
@IsArray({ each: true })
 myArray:MyInnerDto[][];
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jBouyoudcommented, Mar 3, 2020

Hello,

I’ve the same kind of issue with Map and Array values. The test case below show validation error on nestedMap but not on nestedWithArray. Is there another tips to validate this kind of structure ? or it’s a bug ?

With class-validator": "0.11.0"

import { IsString, ValidateNested, Validator } from 'class-validator';

describe('NestedValidation', () => {
  class NestedClass {
    @IsString()
    name: string;

    constructor(name: any) {
      this.name = name;
    }
  }

  class MasterClass {
    @ValidateNested({ each: true })
    nestedMap: Map<string, NestedClass>;

    @ValidateNested({ each: true })
    nestedWithArray: Map<string, NestedClass[]>;

    constructor(
      nestedMap: Map<string, NestedClass>,
      nestedWithArray: Map<string, NestedClass[]>,
    ) {
      this.nestedMap = nestedMap;
      this.nestedWithArray = nestedWithArray;
    }
  }

  describe('validation', () => {
    let validator: Validator;

    beforeAll(() => {
      validator = new Validator();
    });

    it('should validate nested object array', async () => {
      const nestedMap = new Map<string, NestedClass>();
      nestedMap.set('test', new NestedClass(42));
      const nestedMapWithArray = new Map<string, NestedClass[]>();
      nestedMapWithArray.set('test', [new NestedClass(42)]);
      const masterClass = new MasterClass(nestedMap, nestedMapWithArray);

      const errors = await validator.validate(masterClass);

      function expectNestedError(property: string) {
        const nestedError = errors.find(error => error.property === property);
        expect(nestedError).toBeDefined();
        expect(nestedError).toMatchObject({ property, target: masterClass, });
      }
      expectNestedError('nestedMap');
      expectNestedError('nestedWithArray');
    });
  });
});

Thanks

2reactions
mainakchharicommented, Jan 9, 2022

Any word on this? Can anyone suggest a workaround using custom validators that can do the needful? I need to validate multi dimensional arrays of various types, ie. enums, strings, numbers, objectids (mongodb)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question: Passing multidimensional arrays to functions. - Reddit
There are two takeaways here. First, the overarching rule of reading C declarators and expressions is Parens, Postfix, Prefix Second, the ...
Read more >
Multidimensional Arrays - MATLAB & Simulink - MathWorks
A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows...
Read more >
Compare multidimensional arrays - TechNet - Microsoft
I've got a problem with multidimensional arrays. I need to compare two multidimensional arrays as follows: $array1 number word. $array2
Read more >
Multidimensional array terminology - Math Stack Exchange
I know you are asking about arrays and not multidimensional matrices, but perhaps this can help. If you fix an index of the...
Read more >
Multidimensional Arrays | Using Arrays in PHP - InformIT
Arrays do not have to be a simple list of keys and values—each location in the array can hold another array. This way,...
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