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 methods ? #21

Open
mtancoigne opened this issue Apr 19, 2017 · 4 comments
Open

Custom methods ? #21

mtancoigne opened this issue Apr 19, 2017 · 4 comments

Comments

@mtancoigne
Copy link

mtancoigne commented Apr 19, 2017

Hi !
There is an issue with random.number, we can't set a minimum/maximum value:

  • field: {{random.number}} - gives a random number
  • field: {{random.number(x)}} - gives a random value, x being the max
  • faker.random.number({min:x, max:y}) - Gives a randome value between x and y
  • but field: {{random.number({min:x, max:y})}} returns NaN

So I was wondering of there is a possibility to add custom generators to Sails-hook-sanseed, or if it is planned.

Thanks for your work.

Edit The question has been developped below.

@mtancoigne
Copy link
Author

Well, I speaked a bit too quickly. Sorry for that.

@sanjorgek
Copy link
Owner

Sorry, but
what happened?

@mtancoigne
Copy link
Author

Sorry :)
I was implementing the seeds and had issues with faker : it seems impossible to use {{random.number}} with a min and a max value (I wanted to use it to generate random ids for models relations). It only works with the max value ({{random.number(max)}}). So my question was about implementing the possibility to pass a method or something, to replace the fakers one.

That would allow us to create some custom generators too...

But for now, i don't really know the state of this issue, as i'm really, really tired...

Whatever, do you think it's possible to implement something like this in the future ?

@mtancoigne
Copy link
Author

Hi!

Here, i'm back with a more constructive explanation.

For example, I have this seed:

       question: {
        faker: {
          format: {
            level: "{{random.number(5)}}", // level is valid in a range of 1-5, so it won't work as zeros are generated
            // level: `${getRandomInt(1, 5)}`, // won't work too, as only evaluated once
            question: "{{lorem.words(5)}}?",
            answer: "{{random.number(3)}}",
            answers: [  // this won't work either, the array is created with "{{lorem.words(10).}}" strings
              "{{lorem.words(10).}}",
              "{{lorem.words(10).}}",
              "{{lorem.words(10).}}",
              "{{lorem.words(10).}}",
            ],
          },
          locale: "fr",
          quantity: seedConf.pledges,
          oneTo: {
            question_category: {
              id: getRandomInt(1, seedConf.questionCategories), // This is a custom method wich runs fine as not a faker input
            }
          },
          manyTo: {},

        },
        migrate: "drop"
      },

The idea is to be able to pass custom data generators methods to to properties, maybe a callback or something...

fakeField: function() {return specific_generator(some, custom, params)},

Then in the hook, you may test if the field is a string or a method and use faker or call the method accordingly...

What do you think ?

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

No branches or pull requests

2 participants