Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic property support #102

Open
tlvince opened this issue Nov 14, 2019 · 3 comments · May be fixed by #120
Open

Dynamic property support #102

tlvince opened this issue Nov 14, 2019 · 3 comments · May be fixed by #120

Comments

@tlvince
Copy link

tlvince commented Nov 14, 2019

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
        }
      }
    }
  }
}
@bermann
Copy link

bermann commented Feb 8, 2020

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

@tlvince
Copy link
Author

tlvince commented Feb 9, 2020

Nope. As a workaround, I rolled my own.

@Pixelatex
Copy link

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

@josh-hemphill josh-hemphill linked a pull request Aug 1, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants