JSON schema, oneOf validation -
my login endpoint can have "social" logins , "user" login body either this:
user: { email: 'test@gmail.com', password: 'test' }
or this:
authentication: { provider: 'facebook', token: 'abcd' }
i want create json schema validates either first body, or second body. stumbled upon oneof
, have tried:
body: { oneof: [{ properties: { user: { type: 'object', required: [ 'email', 'password' ], email: { type: 'string', format: 'email' }, password: { type: 'string', minlength: 6, maxlength: 32 } }, } }, { properties: { authentication: { type: 'object', required: [ 'provider', 'token' ], provider: { type: 'string', enum: ['facebook'] }, token: { type: 'string' } } } }] }
however gives me following error:
{ "keyword": "oneof", "datapath": ".body", "schemapath": "#/properties/body/oneof", "params": {}, "message": "should match 1 schema in oneof" }
would awesome if can me out making json schema setup this. thanks
you can oneof
. although use differently.
{ "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "login": { "oneof": [ { "required": [ "user" ] }, { "required": [ "authentication" ] } ], "type": "object", "properties": { "user": { "type": "object", "properties": { "email": { "type": "string" }, "password": { "type": "string" } } }, "authentication": { "type": "object", "properties": { "email": { "type": "string" }, "password": { "type": "string" } } } } } } }
with this, 2 json validated against schema
{ "login": { "authentication": { "provider": "facebook", "token": "abcd" } } } { "login": { "user": { "email": "test@gmail.com", "password": "test" } } }
but if try put both of them in one, not validate
{ "login": { "authentication": { "provider": "facebook", "token": "abcd" }, "user": { "email": "test@gmail.com", "password": "test } } }
Comments
Post a Comment