-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Base64 Encoding data #661
Comments
The Json Schema specification does not define "base64" as a format specifier. So trying that isn't going to work unless the chosen schema validator has a custom implementation for a custom "base64" format. On the other hand, the Json Schema spec defines "base64" as a content encoding for the
Yes. RFC4648-conforming Base64 is a rather simple data format with a very specific and limited set of characters, arranged in 4-character tuples (https://datatracker.ietf.org/doc/html/rfc4648#section-4 ). This can be relatively easily modeled by a regex pattern (using the The case-insensitive pattern for one full non-padded 4-char tuple is basically: The last 4-char tuple (or the single tuple if the decoded data is <= 3 bytes in length) however can be a full 4-char tuple or padded tuple in the form of XX== or XXX=, the regex pattern for the last tuple in the encoded Base64 string thus being (with white-spaces for legibility): Therefore, the full regex pattern that matches non-empty Base64-strings that consist of one or more 4-character tuples:
If the schema you are designing should be interoperable/compatible with various different schema validators (which is probably a good idea to follow regardless of whether you have this particular requirement right now or not), it's preferable to not use the
(No guarantee about me not making a silly error here when plonking down the regex pattern here. Please do verify it yourself before putting it into production code...) |
Thank you for the detailed response. The regex works fine :)
|
Please read through the documentation. You will find that @elgonzo's response is correct. Another solution is to create a custom format. Information for doing so can be found in the linked documentation. |
Documentation
Nuget Package
JsonSchema.Net
Package Version
3.0.15
How can I help? Please provide as much context as possible.
I have a field in json schema that requires the string to be in base64 encoded.
I tried to put this in format:
"definitions": {
"File": {
"type": "object",
"properties": {
"subfield1": {
"type": "string",
“format”: “base64”
}
}
}
}
but when i try to validate using the playground https://json-everything.net/json-schema , it does not work correctly even if I put invalid base64 data. I have tried using "contentEncoding": "base64" also not working. Any ideas how to make it work?
Thanks
Code of Conduct
The text was updated successfully, but these errors were encountered: