blahblah
blahblah

Reputation: 1018

JSON schema validation for base64 string is not working

I am using following schema to validate image base64 string:

 {
   "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   "properties": {
     "image": {
       "type": "string",
       "media": {
         "contentEncoding": "base64",
         "oneOf": [
               { "mediaType": "image/png" },
               { "mediaType": "image/jpg" },
               { "mediaType": "image/jpeg" }
         ]
       }
     }
   },
   "required": [
     "image"
   ]
 }

But the schema is not working, any string, even invalid, is passed to application

I am using JSON Schema for PHP to validate json input

UPDATE

This what I tried to change, but it doesn't work

{
 "$schema": "http://json-schema.org/draft-04/schema#",
 "type": "object",
 "properties": {
   "image": {
     "title": "image",
     "type": "string",
     "media": {
       "binaryEncoding": "base64",
       "type": "image/png"
     }
   }
 },
 "required": [
   "image"
 ]
 }

Upvotes: 4

Views: 7141

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

Your problem is old syntax which is no longer supported in JSON Hyper-Schema Draft #4. It changed contentEncoding into binaryEncoding (see the change log here).

A relevant excerpt from the spec follows:

    "imgData": {
        "title": "Article Illustration (small)",
        "type": "string",
        "media": {
            "binaryEncoding": "base64",
            "type": "image/png"
        }
    }

Also, you mentioned nothing about which validator you're using. It may prove important, as compliance across different implementations may vary.

Update: Also, the $schema should be http://json-schema.org/draftv4/hyper-schema for hyper-schema processing.

Upvotes: 1

Related Questions