Validation of string byte-length? #377
-
I want to ensure a specific string field that carries textual data does not exceed a specific byte-length. Since strings are UTF-8 encoded, For reference, I would like this schema: {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "string",
"maxLength": 4,
} to fail against this JSON (since the string has 8 bytes): "ãããã" Is there any way to validate this? If not, is it on the roadmap for future json schema releases? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Unfortunately, there's no way to validate this with JSON Schema. I've always thought that the way this keyword is defined (counting visible characters) made it practically useless. Almost always, the reason to constrain the length of a string in a schema is to ensure that it will fit in some space in memory (usually a cell in a relational database table). I'd even call I'd support adding a new keyword that constrains the maximum size of strings. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Unfortunately, there's no way to validate this with JSON Schema. I've always thought that the way this keyword is defined (counting visible characters) made it practically useless. Almost always, the reason to constrain the length of a string in a schema is to ensure that it will fit in some space in memory (usually a cell in a relational database table). I'd even call
maxLength
a foot-gun because people use this keyword all the time to check that their strings will fit in their database tables and don't realize that it won't always work.I'd support adding a new keyword that constrains the maximum size of strings.