tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 262 forks source link

enum not working properly #288

Closed NSLog0 closed 4 years ago

NSLog0 commented 5 years ago

I've defined a scheme which to check data is containing any value by holding value in enum

var Validator = require('jsonschema').Validator

exports.notificationPayloadValidate = (data) => {
  const v = new Validator()

  const payloadSchema = {
    id: '/NotificationPayload',
    type: 'object',
    properties: {
      src: {
        type: 'object',
        serviceName: { type: 'string' },
        required: ['serviceName'],
      },
      body: {
        type: 'object',
        data: { type: 'object' },
        text: { type: 'string' },
        link: { type: 'string' },
      },
      targets: { $ref: '/Target' },
      retire: { type: 'number' },
    },
    required: ['src', 'body', 'targets'],
  }

  const targetsSchema = {
    id: '/Target',
    type: 'array',
    properties: {
      msgType: {
        enum: ['push', 'email'],
      },
      templateId: {
        type: 'string',
      },
      address: {
        type: 'string',
      },
      locale: { type: 'string' },
      required: ['address', 'msgType', 'locale'],
    },
    required: ['msgType', 'address', 'locale'],
  }

  v.addSchema(targetsSchema, '/Target')

  return v.validate(data, payloadSchema)
}

When I run validator I got empty result instead of thrown any error

awwright commented 4 years ago

@NSLog0 Can you submit a minimal test case?

I think this is an error with your schema. Your targetSchema specifies type: "array" but uses the "properties" keyword, which only makes sense with type: "object". Perhaps you intended to use the "items" keyword?