1

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:

var cfStackSchema = {
  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:

var spec = { name: "test", 
            account: "test", 
            environment: "test",
            parameters: "test",
            application: "test"
          };

Here is the code I'm using to validate:

  var ajv = new Ajv({ useDefaults: true });
  var validate = ajv.compile(cfStackSchema);
  if (!validate(spec)) {
    throw new Error('Stack does not match schema!')
  }
2
  • 1
    please include the code you are using to validate, as that is most likely where the problem is Commented Apr 29, 2017 at 19:40
  • Thanks. It has been added. Commented Apr 29, 2017 at 19:44

1 Answer 1

1

You just need to put the properties inside of a properties object

var cfStackSchema = {
  properties: {
    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"]
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.