danielgtaylor / eidolon

Generate JSON or JSON Schema from Refract & MSON data structures
MIT License
6 stars 1 forks source link

Generate smart random default values for examples #7

Closed danielgtaylor closed 8 years ago

danielgtaylor commented 8 years ago

For example, given the following MSON input with no samples or default values:

# Customer
- id
- name
- email
- country
- lastLoginDate
- plan
  - cost (number)
  - enabled (boolean)

You would get something like:

{
  id: 'ae29f4d3-8954-4972-ab72-053d082a3fd6',
  name: 'Haven Kuhlman',
  email: 'Kristina.Koch@yahoo.com',
  country: 'DE',
  lastLoginDate: 'Thu May 07 2015 14:27:18 GMT-0700 (PDT)',
  plan: {
    cost: 826.13,
    enabled: true
  }
}

Of course, providing your own sample or default value will override the generated default, so this is only going to show up in the case with no sample. If no information about context can be guessed for a particular string element, then a simple Lorem Ipsum nonsense word or words will be used.

In order to provide consistent output, there is now a seed option for the example method that sets the Mersenne twister seed value. The tests are written to not depend on this, as the order of returned items may change with subsequent faker.js releases.

Also, you can still manually override all of this magic by providing your own default generator function. For example, this replaces each generated default value with the name of the refract element:

import eidolon from 'eidolon';

function myDefaultValue(refract, path) {
  return refract.element;
}

eidolon.example({ /* ... some refract structure ... */}, [], {
  defaultValue: myDefaultValue
});

cc @Baggz

Baggz commented 8 years ago

@danielgtaylor

Looks good to me! :100:

Do you think you can merge the PR, release a new version, publish that version and update Eidolon in the Attributes Kit and in Apiary as well?

Thanks!