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

add description and examples for maxLength keyword #110

Merged
merged 13 commits into from
Feb 12, 2024
46 changes: 46 additions & 0 deletions content/2020-12/validation/maxLength.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,49 @@ related:
- vocabulary: format-annotation
keyword: format
---

The `maxLength` keyword is used to specify the maximum length of a string instance. It is used to enforce a constraint on the maximum number of characters allowed for a string instance.

* Applies only to string data types.
* Value must be a non-negative integer.
* String length is counted in characters, not bytes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you mention it (which is a good point), I think it'd be good exemplifying this. I guess you can provide an instance that consists of unicode characters in the UTF-16 and/or UTF-32 range, showing that it still validates against maxLength based on the number of characters.

I recall seeing some examples of this in the JSON Schema Test Suite

* Validation succeeds if the string length is less than or equal to the specified `maxLength`.

## Examples

{{<schema `Schema restricting string length to a maximum of 10 characters`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"maxLength": 10
}
{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 10 is valid`>}}
"foo"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 10 is invalid`>}}
"This is an invalid string"
{{</instance-fail>}}

{{<schema `Schema allowing either a string with a maximum of 20 characters or a numeric value`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": [ "string", "number" ],
"maxLength": 20
}

{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 20 is valid`>}}
"This is a valid string"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 20 is invalid`>}}
"This description is too long"
{{</instance-fail>}}

{{<instance-pass `An instance with a numeric value is valid`>}}
55
{{</instance-pass>}}
Loading