voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 242 forks source link

How I can set string format by uri or email? #351

Closed tit closed 7 years ago

tit commented 7 years ago

I have some JSON, ex:

{
  "foo" : "foo@example.com"
}

I want create JSON schema for this JSON.

{
  "type" : "object",
  "properties" : {
    "foo" : {
      "type" : "string",
      "format" : "email"
    }
  }
}

Is this acceptable? But it don't work. :(

RST-J commented 7 years ago

If you carefully read the README, you could have found out that this does not work because it is not supported. The fromat keyword is not mandatory in the JSON schema spec and it is ridiculously complicated to verify an email's syntactical conformance to the RFC - it is not only about having exactly one @ (actually, there may be more), some local name and some domain.

We decided that everything apart from a standard compliant check (which is not worth it to us - maybe someone will change our minds) would restrict the check in an unacceptable way.

But, also described in the README (surprise!) is how to add and use custom format validators with json-schema with which you could add a validation to check for compliance with your particular view of what a correct email is. "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com is a valid email which you probably nevertheless would not like to validate, maybe or maybe you want to exclude display names as in Foo <bar@example.com>...