Schema

The schema is a YAML file that defines the structure of your data. It is used to control the generation of the data.

There are three major components to the schema:

A complete example

This is a mostly complete example of schema, it is missing some field-specific arguments. For more information on the arguments for each field, see the Fields documentation.

It is also missing external data. For more information on external data, see the External Data documentation.

As with all schema's this can be used to output JSON or CSV data. However, in this case JSON is used as it is easier to show the complexity of the nested fields in the output.

Definition

fields:
  # Generate a number between 0 and 10.
  - name: A
    type: IntegerInRange
    args:
      min: 0
      max: 10
  # Generate a number between 0 and 20 that is greater than A.
  - name: B
    type: IntegerInRange
    args:
      max: 20
    constraints:
      - type: GreaterThan
        name: A
  # Generate a sentence if E is null.
  - name: C
    type: String
    args:
      subtype: Sentences
    constraints:
      - type: IfNull
        name: E
  # Generate a random boolean.
  - name: D
    type: Boolean
  # Generate a datetime with a 10% probability (90% chance of being null).
  - name: E
    type: DateTime
    null_probability: 0.9
  # Generate a string from a list of choices with unequal probability.
  - name: F
    type: WeightedCategory
    args:
      choices:
        - ["FOO", 2]
        - ["BAR", 1]
  # Generate a string from a list of choices with equal probability.
  - name: G
    type: WeightedCategory
    args:
      choices:
        - "FOO"
        - "BAR"
  # Generate a string with random substitutions.
  - name: H
    type: Bothify
    args:
      format: "RANDOM_ID: ??-##-??"
  # Generate a nested object.
  - name: I
    type: Nested
    fields:
      - name: J
        type: IntegerInRange
        args:
          min: 0
          max: 10
      - name: K
        type: IntegerInRange
        args:
          min: 0
          max: 11
        constraints:
          - type: GreaterThan
            name: A
      - name: L
        type: IntegerInRange
        args:
          min: 0
          max: 13
      - name: M
        type: Nested
        fields:
          - name: N
            type: IntegerInRange
            args:
              min: 0
              max: 12
          - name: O
            type: IntegerInRange
            args:
              min: 0
              max: 13
            # Reference a nested field.
            constraints:
              - type: GreaterThan
                name: I.M.N
  # Generate a list of nested objects.
  - name: P
    type: Nested
    args:
      subtype: List
    fields:
      - name: Q
        type: Nested
        fields:
          - name: R
            type: IntegerInRange
            args:
              min: 0
              max: 10
      - name: S
        type: Nested
        fields:
          - name: T
            type: IntegerInRange
            args:
              min: 0
              max: 10
  # Demonstrate templating and referencing other fields.
  - name: U
    type: FormattedString
    args:
      format: |
        This is the value of A: {{ refs['A'].raw }}
        This is the value of A + 10: {{ refs['A'].raw + 10 }}
  # Generate and array of integers of varying length.
  - name: V
    type: Array
    args:
      min: 0
      max: 5
      field:
        name: W
        type: IntegerInRange
        args:
          min: 0
          max: 10
  # Generate a "FreeEmail" by deferring the `fake-rs` library.
  - name: X
    type: Fakers
    args:
      subtype: FreeEmail
  # Calculate a duration between two dates, generally used by referencing other fields.
  - name: Y
    type: Duration
    args:
      start: "2010-01-01T00:00:00Z"
      end: "2020-01-01T00:00:00Z"
      component: Years
  # Look up a value in a map. Used by referencing other fields, Map's are
  # defined in the `maps` section and are reference by name.
  # - name: Z
  #   type: Map
  #   args:
  #     from_map: ID_TO_NAME
  #     key: A
  #     default: "No value found"

Output

fodder -s schema.yaml -n 1 -f json
[
  {
    "A": 3,
    "B": 4,
    "C": "dolorem praesentium rerum vel ipsum dolorum veritatis.",
    "D": false,
    "E": null,
    "F": "BAR",
    "G": "BAR",
    "H": "RANDOM_ID: zS-98-TO",
    "I": {
      "J": 3,
      "K": 4,
      "L": 1,
      "M": {
        "N": 3,
        "O": 9
      }
    },
    "P": [
      {
        "R": 3
      },
      {
        "T": 1
      }
    ],
    "U": "This is the value of A: 3\nThis is the value of A + 10: 13\n",
    "V": [],
    "X": "kyle_excepturi@gmail.com",
    "Y": "10"
  }
]