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

[question] [discussion] [help needed] Generating json object with random key #77

Closed
hellboy81 opened this issue Oct 20, 2015 · 8 comments

Comments

@hellboy81
Copy link

How can I generate object with random key that has following schema:

{
            type: 'string',
            chance: {
                hash: {
                    casing: 'upper',
                    length: 32
                }
            }
}

Key should be generated on the basis of previous schema (random upper-cased string with length 32)

{
   "110B949E3BF0FE7C0DC15A17F04FA53E": { // Fake object is generating based on another schema 
      ... // Some fields
    }
}
@ducin
Copy link
Contributor

ducin commented Oct 20, 2015

Hi @hellboy81, you can't do that in one step, I mean, json-schema-faker doesn't allow you to do such thing out of the box.

But if you do need such feature, you might just divide generating above document into two steps:

  • generate the key (with what you do have already) and store it as a string
  • use the key as a part of a dynamically-generated json-schema (combine it using a node.js script or whatever else)
    This will do the trick.

There is very little chance we'd like to implement such feature into json schema faker. And that's because jsf is just a tiny superset of json-schema standard: we add very little to the standard, but provide big functionality. The more we stick to the standard, the more other javascript tools could integrate with jsf. The more we distance from the schema, the more difficult it'd be to integrate.

Do you need something further?

@pateketrueke
Copy link
Member

Or you can use patternProperties as follows:

{
  "type": "object",
  "patternProperties": {
    "[A-Z\\d]{32}": {
      "type": "string"
    }
  }
}

As it follows the json-schema standards I think it would be enough?

@ducin
Copy link
Contributor

ducin commented Oct 20, 2015

@pateketrueke didn't know about this feature. Now I have a question here - how can we make this dynamically generated attribute required? I've pasted your code into our demo and it randomly gets either the dynamic thing generated or whole being empty.

@pateketrueke
Copy link
Member

I've missed it but using minProperties does the trick:

{
  "type": "object",
  "patternProperties": {
    "[A-Z\\d]{32}": {
      "type": "string"
    }
  },
  "minProperties": 1
}

@hellboy81
Copy link
Author

@ducin OK, this is not a part of JSON-Schema specification draft-04

@ducin
Copy link
Contributor

ducin commented Oct 20, 2015

@hellboy81 ORLY? Look at this, the find (ctrl+f): patternProperties - it is a part of the json schema validation. At least that's what I can see.

@pateketrueke not sure what you mean: you missed it, like, you can't remind what was the right attribute to set, but it does exist? Or you don't know whether it exists or not? ;) the trick you wrote above works for simple cases, but it doesn't seem to scale, if you want to customize the object with required attributes and optional attributes and all the rest that json schema provides.

@pateketrueke
Copy link
Member

@ducin yeah, sorry for that, I've missed that property in the given example.

And yeah, is very difficult to ensure required patternProperties all the time, I think we need to explore edge cases for that. ;)

After some research most people say that using min/max properties would be a solution.

I didn't get a better idea and JSON-schema is not clear about that.

@ducin
Copy link
Contributor

ducin commented Oct 20, 2015

Seems like @hellboy81 's question leads to a gap in json schema standard. @pateketrueke no worries! 🍻

@ducin ducin added this to the v0.3.2 milestone Apr 20, 2016
@ducin ducin self-assigned this Apr 20, 2016
@ducin ducin removed their assignment Apr 20, 2016
@ducin ducin removed this from the v0.3.2 milestone Apr 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants