eivindfjeldstad/validate

Dynamic property support

Opened this issue · 3 comments

I have a use case where the top-level object property can be anything (e.g. a UUID) with nested properties which must conform to a schema, e.g.:

const users = {
    abc: {
        name: 'Foo'
    },
    def: {
        name: 'Bar'
    }
}

Does validate support this? I've tried the following:

var Schema = require("validate")

const schema = new Schema({
    [/[a-z].+/]: {
        name: {
            type: String,
            required: true
        }
    }
})

schema.validate({a: { name: 'foo' }})
//=> [ValidationError: /[a-z].+/.name is required.]

In JSON schema, this works:

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-03/schema",
  "required": true,
  "patternProperties": {
    "^[a-z].+$": {
      "type": "object",
      "required": true,
      "properties": {
        "name": {
          "type": "string",
          "required": true
        }
      }
    }
  }
}

@tlvince I'm having the same issue, have you found any solution to this?

Nope. As a workaround, I rolled my own.

@tlvince you rolled your own? A validator function or a replacement for 'validate' altogether?
I have the exact same issue :/