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

Custom walker #841

Closed
alturkovic opened this issue Jul 22, 2023 · 2 comments · Fixed by #986
Closed

Custom walker #841

alturkovic opened this issue Jul 22, 2023 · 2 comments · Fixed by #986

Comments

@alturkovic
Copy link

I am a bit confused by the walk functionality and how to implement it...

I want to walk the following schema to detect which properties have non-standard faker field:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "faker": "name.findName"
    },
    "email": {
      "type": "string"
    }
  }
}

How would I implement a JsonSchemaWalker to detect such fields?

For the payload below, I would like to pass the value of the name from the input object together with my custom faker field value. I would like to call something like faker.fake(input, jsonPointerToName, "name.findName").

{
  "name": "John Doe",
  "email": "john.doe@gmail.com"
}

This seems similar to "Applying defaults" functionality already present in the library, but instead of applying defaults, I would like to pass the current value of the field, where it is located at (so I can replace it), and the value of the faker field so I know how to generate its replacement.

@fdutton
Copy link
Contributor

fdutton commented Jul 29, 2023

@alturkovic This project focuses on validation and is not really suitable for transformations. You may be able to achieve your goal by using walkers, listeners and collectors but I suspect the solution will be quite complicated as you would have to deal with transitions across objects, arrays, properties, items and other schemas. For example, consider this simple refactor of your schema. Your solution would have to take these types of constructs into consideration. I recommend looking for another solution before pursuing this one.

{
  "type": "object",
  "properties": {
    "name": { "$ref": "#/$defs/Name" },
    "email": { "type": "string" }
  },
  "$defs": {
    "Name": {
      "type": "string",
      "faker": "name.findName"
    }
  }
}

You can find an examples of how to use JsonSchemaWalker, JsonSchemaWalkListener and Collector in several unit-tests.

@alturkovic
Copy link
Author

I would not worry about refs since I could always resolve them beforehand and inline all the refs.

To be more specific, I am trying to implement something like: https://json-schema-faker.js.org/#gist/1902737e7bef9573af02a3fc49761c13 and this library seemed like a way to go using walkers, but I am not sure if my assumption was correct. I am having difficulties recognizing which property is both required and has the "faker" extension.

Would you advise against using this library for such usecase?

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.

2 participants