deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

[type] Validation incorrectly errors if keys are stringified numbers #151

Closed icopp closed 2 years ago

icopp commented 3 years ago

A simple test case for the problem:

import { t, validatedPlainToClass } from '@deepkit/type'

class TestClass {
  @t.string '1': string
  @t.string '2': string
  @t.string '3': string
}

validatedPlainToClass(TestClass, {
  '1': 'string1',
  '2': 'string2',
  '3': 'string3',
})

This results in an error:

    SyntaxError: Unexpected number
        at new Function (<anonymous>)

       7 | }
       8 |
    >  9 | validatedPlainToClass(TestClass, {
         |                      ^
      10 |   '1': 'string1',
      11 |   '2': 'string2',
      12 |   '3': 'string3',

The transformation without the validation doesn't error:

plainToClass(TestClass, {
  '1': 'string1',
  '2': 'string2',
  '3': 'string3',
})

However, invoking validate at any point on that object then results in the same error.

marcj commented 3 years ago

Accessing properties in the validation codes is done via data.${property.name}, see https://github.com/deepkit/deepkit-framework/blob/master/packages/type/src/jit-validation.ts#L292. In the serialiser this happens via _instance[${JSON.stringify(property.name)}], see https://github.com/deepkit/deepkit-framework/blob/master/packages/type/src/jit.ts#L492.

The first needs to be adjusted accordingly to support numeric property names.

marcj commented 2 years ago

Closing since inactivity and old unsupported version. This should be supported in the newest version though