Fields

Fields are the core of the schema, they define the structure of the data that will be generated. They are defined in the fields section of the schema.

A simple example

Below is a simple example of a schema with a single field. This field contains most of the possible options that can be set for a field, excluding Constraints and some features that are only available for certain field types, e.g Nested fields and fields that allow Reference constraints.

Some details about this example:

  • This field is named ID and this is how it will be named in the output.
  • It is of type IntegerInRange meaning it will generate a random integer within a range.
  • It has two (optional) arguments, min and max. These are used to define the range of the integers that will be generated. In this case it is showing the default values of 0 and 9223372036854775807 (i64::MAX).
  • It has a null_probability of 0 meaning that it will never generate a null value.
fields:
  - name: ID
    type: IntegerInRange
    args:
      min: 0
      max: 9223372036854775807
    null_probability: 0

Running the above schema through fodder will generate the following output (in JSON format):

fodder -s schema.yaml
[
  {
    "ID": 4350876185243800642
  },
  {
    "ID": 3998117975203216203
  },
  {
    "ID": 2709943470313341799
  }
]

A more complex example (with constraints)

Below is a more complex example of a schema with multiple fields. This example shows how to use Constraints to ensure that the generated data is representative of the real world.

Some details about this example:

  • All fields will generate a random DateTime.
  • The CreatedAt field will generate a random DateTime between 3 days ago and today.
  • The ModifiedAt field will generate a random DateTime that is greater than CreatedAt and between 3 days ago and today.
  • The DeletedAt field will generate a random DateTime that is greater than ModifiedAt and between 3 days ago and today. It will also have a null_probability of 0.9 meaning that it will have a 90% chance of being null.
fields:
  - name: CreatedAt
    type: DateTime
    args:
      start: -3d
      end: today
      format: "%Y-%m-%d %H:%M:%S"
  - name: ModifiedAt
    type: DateTime
    args:
      start: -3d
      end: today
      format: "%Y-%m-%d %H:%M:%S"
    constraints:
      - type: GreaterThan
        name: CreatedAt
  - name: DeletedAt
    type: DateTime
    args:
      start: -3d
      end: today
      format: "%Y-%m-%d %H:%M:%S"
    null_probability: 0.9
    constraints:
      - type: GreaterThan
        name: ModifiedAt

Running the above schema through fodder will generate the following output (this time in CSV format):

fodder -f csv -s schema.yaml
CreatedAtModifiedAtDeletedAt
2023-01-30 12:49:542023-01-31 19:45:542023-02-01 22:50:54
2023-01-30 11:57:542023-01-31 22:59:54
2023-02-01 10:27:542023-02-01 23:22:54