packages/tyranid/src/fake.js
function fakeField(field) {
  const def = _.get(field, 'is.def') || field.def;

  if (!def)
    throw new Error(
      'No field.def property to fake on! ' + JSON.stringify(field)
    );

  switch (def.name) {
    case 'integer':
    case 'float':
    case 'double':
    case 'number':
      return faker.random.number();

    case 'email':
      return faker.internet.email();

    case 'url':
      return faker.internet.url();

    case 'date':
      const date = faker.date.past();
      date.setMilliseconds(0);
      return date;

    case 'image':
      return faker.image.avatar();

    case 'link':
    case 'mongoid':
      let i = 24,
        s = '';
      while (i--) s += faker.random.number(15).toString(16);
      return ObjectId(s);

    case 'boolean':
      return faker.random.boolean();

    case 'array':
      return _.range(2).map(() => fakeField(field.of));

    case 'object':
      const key = def.name === 'link' ? 'link.def.fields' : 'fields';

      return _.reduce(
        _.get(field, key),
        (out, value, key) => {
          out[key] = fakeField(value);
          return out;
        },
        {}
      );

    // default to string
    default:
      return faker.name.lastName();
  }
}
function fakeField(field) {
  const def = _.get(field, 'is.def') || field.def;

  if (!def)
    throw new Error(
      'No field.def property to fake on! ' + JSON.stringify(field)
    );

  switch (def.name) {
    case 'integer':
    case 'float':
    case 'double':
    case 'number':
      return faker.random.number();

    case 'email':
      return faker.internet.email();

    case 'url':
      return faker.internet.url();

    case 'date':
      const date = faker.date.past();
      date.setMilliseconds(0);
      return date;

    case 'image':
      return faker.image.avatar();

    case 'link':
    case 'mongoid':
      let i = 24,
        s = '';
      while (i--) s += faker.random.number(15).toString(16);
      return ObjectId(s);

    case 'boolean':
      return faker.random.boolean();

    case 'array':
      return _.range(2).map(() => fakeField(field.of));

    case 'object':
      const key = def.name === 'link' ? 'link.def.fields' : 'fields';

      return _.reduce(
        _.get(field, key),
        (out, value, key) => {
          out[key] = fakeField(value);
          return out;
        },
        {}
      );

    // default to string
    default:
      return faker.name.lastName();
  }
}
function fakeField ( field ) { const def = _ . get ( field , 'is.def' ) || field . def ; if ( ! def ) throw new Error ( 'No field.def property to fake on! ' + JSON . stringify ( field ) ) ; switch ( def . name ) { case 'integer' : case 'float' : case 'double' : case 'number' : return faker . random . number ( ) ; case 'email' : return faker . internet . email ( ) ; case 'url' : return faker . internet . url ( ) ; case 'date' : const date = faker . date . past ( ) ; date . setMilliseconds ( 0 ) ; return date ; case 'image' : return faker . image . avatar ( ) ; case 'link' : case 'mongoid' : let i = 24 , s = '' ; while ( i -- ) s += faker . random . number ( 15 ) . toString ( 16 ) ; return ObjectId ( s ) ; case 'boolean' : return faker . random . boolean ( ) ; case 'array' : return _ . range ( 2 ) . map ( ( ) => fakeField ( field . of ) ) ; case 'object' : const key = def . name === 'link' ? 'link.def.fields' : 'fields' ; return _ . reduce ( _ . get ( field , key ) , ( out , value , key ) => { out [ key ] = fakeField ( value ) ; return out ; } , { } ) ; // default to string default : return faker . name . lastName ( ) ; } }
Recurse into schema and populate fake document
Recurse into schema and populate fake document