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
IDand this is how it will be named in the output. - It is of type
IntegerInRangemeaning it will generate a random integer within a range. - It has two (optional) arguments,
minandmax. These are used to define the range of the integers that will be generated. In this case it is showing the default values of0and9223372036854775807(i64::MAX). - It has a
null_probabilityof0meaning that it will never generate anullvalue.
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
CreatedAtfield will generate a randomDateTimebetween 3 days ago and today. - The
ModifiedAtfield will generate a randomDateTimethat is greater thanCreatedAtand between 3 days ago and today. - The
DeletedAtfield will generate a randomDateTimethat is greater thanModifiedAtand between 3 days ago and today. It will also have anull_probabilityof0.9meaning that it will have a 90% chance of beingnull.
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
| CreatedAt | ModifiedAt | DeletedAt |
|---|---|---|
| 2023-01-30 12:49:54 | 2023-01-31 19:45:54 | 2023-02-01 22:50:54 |
| 2023-01-30 11:57:54 | 2023-01-31 22:59:54 | |
| 2023-02-01 10:27:54 | 2023-02-01 23:22:54 |