Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: $value is not parsed and replaced in error messages #1046

Closed
willin opened this issue Apr 30, 2021 · 4 comments
Closed

fix: $value is not parsed and replaced in error messages #1046

willin opened this issue Apr 30, 2021 · 4 comments
Labels
status: fixed Issues with merged PRs, but not released yet. type: fix Issues describing a broken feature.

Comments

@willin
Copy link

willin commented Apr 30, 2021

Description

v0.13.1

Minimal code-snippet showcasing the problem

import { MinLength, Matches, validateSync } from 'class-validator';

class Demo {
  @MinLength(10, {
    // here, $constraint1 will be replaced with "10", and $value with actual supplied value
    message: 'Title is too short. Minimal length is $constraint1 characters, but actual is $value'
  })
  @Matches(/^$/, {
    message: '$target, $property, $value, $constraint1'
  })
  name: string;

  constructor(obj: any) {
    Object.assign(this, obj);
  }
}

const demo = new Demo({
  name: true
});

console.log(
  validateSync(demo, {
    // dismissDefaultMessages: true
  })
);
console.log(Object.values(validateSync(demo)[0].constraints));

Expected behavior

$value should be parsed.

Actual behavior

[
  ValidationError {
    target: Demo { name: true },
    value: true,
    property: 'name',
    children: [],
    constraints: {
      matches: 'Demo, name, $value, /^$/',
      minLength: 'Title is too short. Minimal length is 10 characters, but actual is $value'
    }
  }
]
[
  'Demo, name, $value, /^$/',
  'Title is too short. Minimal length is 10 characters, but actual is $value'
]
@willin willin added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Apr 30, 2021
@c0d3ster
Copy link

Also experiencing this issue on a nested object validation if anyone has found a solution?

@pmirand6
Copy link

I was going crazy with this, until I discovered that the message can be used as a function.
All you have to do is to use the message as function, and then access to the value passed:

image

Here you can see an example.

Hope it's Help.

At the moment, I'm still trying to remove the prefix from error message can't

@NoNameProvided NoNameProvided added status: duplicate Issue is being tracked already in another issue. and removed status: needs triage Issues which needs to be reproduced to be verified report. labels Dec 9, 2022
@NoNameProvided
Copy link
Member

Duplicate of #921.

@NoNameProvided NoNameProvided changed the title [Bug] $value is not parsed in message fix: $value is not parsed in error messages Dec 9, 2022
@NoNameProvided NoNameProvided changed the title fix: $value is not parsed in error messages fix: $value is not parsed and replaced in error messages Dec 9, 2022
@NoNameProvided NoNameProvided added status: fixed Issues with merged PRs, but not released yet. and removed status: duplicate Issue is being tracked already in another issue. labels Dec 15, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: fixed Issues with merged PRs, but not released yet. type: fix Issues describing a broken feature.
Development

No branches or pull requests

4 participants