Saturday 29 April 2017

Json Schema: Validating Arrays

I'm trying to validate a schema that includes two keys, tags and parameters, which are intended to be arrays of arbitrary key-value pairs. For some reason, though, I can't get anything I specify for these two keys to fail validation (I'm using the nodejs library ajv).

Here is the schema definition:

{
  name: { type: "string" },
  application: { type: "string" },
  account: { type: "string" },
  environment: { type: "string" },
  tags: { 
    type: "array",
    items: {
      type: "object",
      patternProperties: {
        "^[a-zA-z0-9]$": { type: "string" }
      },
      additionalProperties: false
    },
    additionalItems: false
  },
  parameters: { 
    type: "array",
    items: {
      type: "object",
      patternProperties: {
        "^[a-zA-z0-9]$": { type: "string" }
      },
      additionalProperties: false
    },
    additionalItems: false
  },
  deps: { type: "array", items: { type: "string" } },
  required: ["name", "account", "environment", "parameters", "application"]
}

And here is a test object. I'm passing parameters here as a simple string, intending it to fail validation, but it actually passes:

{ name: "test", 
            account: "test", 
            environment: "test",
            parameters: "test",
            application: "test"
          }

Please tell me what I'm doing wrong. Thanks.



via rumdrums

No comments:

Post a Comment