JSON schema, oneOf validation

Viewed 2086

My login endpoint can have "social" logins and "user" login so the body could either look like this:

user: {
  email: 'test@gmail.com',
  password: 'test'
}

Or like this:

authentication: {
  provider: 'facebook',
  token: 'abcd'
}

I want to create a JSON schema that validates either the first body, or the second body. I stumbled upon oneOf and this is what I 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 that gives me the following error:

    {
        "keyword": "oneOf",
        "dataPath": ".body",
        "schemaPath": "#/properties/body/oneOf",
        "params": {},
        "message": "should match exactly one schema in oneOf"
    }

Would be awesome if anyone can help me out with making a JSON schema setup for this. Thanks

1 Answers
Related