Skip to content

$-sign in object keys are escaped in the ValidationError message #921

@wadebiffen

Description

@wadebiffen

Context

  • node version: 6.2.1
  • joi version: 8.4.2
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

I am trying to validate objects with a $-sign in the key name and expect the validation error message to reference the same key name, but the $-sign is escaped in the error message (it is not escaped in the path or the _object though). I would prefer it to not be escaped when I return the ValidationError object as JSON response directly.

Is this intentional, and if so, why is it not escaped the other places?

'use strict';
const Joi = require('joi');
const schema = {
    $top: Joi.number().integer().min(1)
};
const obj = {
    $top: 0
};
let result = Joi.validate(obj, schema);
console.log(result.error);

Which result you had ?

$top is escaped to "$top"

{ ValidationError: child "$top" fails because ["$top" must be larger than or equal to 1]
  isJoi: true,
  name: 'ValidationError',
  details:
   [ { message: '"$top" must be larger than or equal to 1',
       path: '$top',
       type: 'number.min',
       context: [Object] } ],
  _object: { '$top': 0 },
  annotate: [Function] }

What did you expect ?

I expect $top to not be escaped (or escaped everywhere?)

{ ValidationError: child "$top" fails because ["$top" must be larger than or equal to 1]
  isJoi: true,
  name: 'ValidationError',
  details:
   [ { message: '"$top" must be larger than or equal to 1',
       path: '$top',
       type: 'number.min',
       context: [Object] } ],
  _object: { '$top': 0 },
  annotate: [Function] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changesChange that can breaking existing codefeatureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions