vinejs / vine

VineJS is a form data validation library for Node.js
https://vinejs.dev
MIT License
1.1k stars 22 forks source link

Error when object key is a number #63

Open lousolverson opened 3 months ago

lousolverson commented 3 months ago

Package version

2.1.0

Describe the bug

I am looking to use the following scheme:

export const days_availablesValidator = vine.compile(
  vine.object({
    day_init : vine.string().regex(/^\d{4}\-\d{2}\-\d{2}$/),
        day_end : vine.string().regex(/^\d{4}\-\d{2}\-\d{2}$/).nullable(),
        days_of_week : vine.object({
            '0' : vine.string(),
            '1' : vine.string(),
            '2' : vine.string(),
            '3' : vine.string(),
            '4' : vine.string(),
            '5' : vine.string(),
            '6' : vine.string()
        })
    })
)

and it produces the following error:

[16:26:58.517] ERROR (13439): Numeric separator can not be used after leading 0.
    request_id: "vy66l4s8pvs5e8qm2ols2llg"
    x-request-id: "vy66l4s8pvs5e8qm2ols2llg"
    err: {
      "type": "SyntaxError",
      "message": "Numeric separator can not be used after leading 0.",
      "stack":
          SyntaxError: Numeric separator can not be used after leading 0.
              at new AsyncFunction (<anonymous>)
              at Compiler.#toAsyncFunction (path/to/my/project/node_modules/@vinejs/compiler/src/compiler/main.ts:119:12)
              at Compiler.compile (path/to/my/project/node_modules/@vinejs/compiler/src/compiler/main.ts:181:33)
              at VineValidator (path/to/my/project/node_modules/@vinejs/vine/src/vine/validator.ts:120:8)
              at Vine.compile (path/to/my/project/node_modules/@vinejs/vine/src/vine/main.ts:67:12)
              at <anonymous> (path/to/my/projectapp/validators/secretariats_controller.ts:49:37)
              at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
              at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:485:26)
              at importDefault (path/to/my/project/node_modules/@poppinss/utils/src/import_default.ts:19:25)
              at Object.handle (path/to/my/project/node_modules/@adonisjs/fold/src/module_importer.ts:157:29)
      "status": 500
    }

Finally if I make the following changes it solves the problem:

export const days_availablesValidator = vine.compile(
    vine.object({
        day_init : vine.string().regex(/^\d{4}\-\d{2}\-\d{2}$/),
        day_end : vine.string().regex(/^\d{4}\-\d{2}\-\d{2}$/).nullable(),
        days_of_week : vine.object({
            'd0' : vine.string(),
            'd1' : vine.string(),
            'd2' : vine.string(),
            'd3' : vine.string(),
            'd4' : vine.string(),
            'd5' : vine.string(),
            'd6' : vine.string()
        })
    })
)

Thanks a lot for any solution

Reproduction repo

No response

mekto commented 3 months ago

It could be connected to this issue. If the field name contains dashes the schema won't compile.

v.compile(
  v.object({
    'g-recaptcha-response': v.string().optional(),
  })
);

throws

SyntaxError: Missing initializer in const declaration
    at new AsyncFunction (<anonymous>)