joi icon indicating copy to clipboard operation
joi copied to clipboard

Documentation "Strings are utf-8 encoded by default." is misleading

Open telaoumatenyanis opened this issue 3 years ago • 0 comments

Context

  • node version: v12.21.0
  • module version: "^17.4.0

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

First excuse me if I am making false assumptions, I only have a surface-level knowledge of the code base.

The documentation states the following:

When validating a schema: ...

  • Strings are utf-8 encoded by default.

From the code, here is how the length seems to be inferred:

const length = encoding ? Buffer && Buffer.byteLength(value, encoding) : value.length; 

If encoding is not defined, the value.length is used to get the length of the string. However, I believe v8 is using UTF16 for its .length property.

I cannot find any default encoding in the code as well.

Here is an example of the result Joi is giving me:

const Joi = require("joi");

const testChar = "😍";

const defaultSchema = Joi.string().max(3);
const utf8Schema = Joi.string().max(3, "utf-8");

console.log("Default encoding");
console.log(defaultSchema.validate(testChar));
console.log("utf-8 encoding");
console.log(utf8Schema.validate(testChar));

Here is the result output:

Default encoding
{ value: '😍' }

utf-8 encoding
{
  value: '😍',
  error: [Error [ValidationError]: "value" length must be less than or equal to 3 characters long] {
    _original: '😍',
    details: [ [Object] ]
  }
}

telaoumatenyanis avatar Mar 05 '21 22:03 telaoumatenyanis