Dynamic property support
Opened this issue · 3 comments
tlvince commented
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 commented
Nope. As a workaround, I rolled my own.