Skip to content

Value format

format

The value format setting.

format.strategy

The strategy how it should use to generate value with specific format way. It would set the value format as BY_DATA_TYPE in default.

The following format strategies are provided by PyFake-API-Server:

Strategy name Purpose
BY_DATA_TYPE Randomly generate value by the setting data type
STATIC_VALUE Generate value from static value only
FROM_ENUMS Randomly generate value from an array
CUSTOMIZE Randomly generate value by customization value which may includes the format variable
FROM_TEMPLATE Use the format setting in template section to randomly generate value

New in version 0.4.2

Add new strategy STATIC_VALUE in version 0.4.2.

format.digit

Set the digit setting of the value.

Not for every value

This property would be great to use for numeric type value only. A non-numeric type value, i.e., pure string type or boolean type value, is not necessary and is not also ineffective with this property.

About more details, please refer to here.

format.size

Set the value size of the value.

Size in different meanings for different types

A size of value could be 2 meanings:

  • For a numeric type value, it means maximum and minimum.
  • For a non-numeric type value like string type one, it means the length of value.

About more details, please refer to here.

format.static_value

Set the specific value be fixed for generating without any changes or modifications.

Activate to use this property with strategy STATIC_VALUE.

Exactly usage

Sometimes we don't want the value is randomly which doesn't be good to test, so we want to fix the value as a specific value.

api.yaml
response:
  strategy: object
  properties:
    - name: data
      required: true
      type: str
      format:
        strategy: static_value
        static_value: OK

The data it generates would always be OK no matter how many you let it generate:

part of result in API response
{ "data": "OK" }

New in version 0.4.2

format.unique_element

If the value be activated, it would generate multiple unique elements for array type property. It accepts a boolean type value. It's false in default.

It recommends that activating to use this property with strategy BY_DATA_TYPE which is array type value.

When to use?

In generally, this property should be only used for array type property. Here provide a simple demonstration by pure value element of array with different :

Set a pure value element of array type property and format.unique_element as false (in default):

api.yaml
response:
  strategy: object
  properties:
    - name: data
      required: true
      type: list
      format:
        unique_element: false
        size:
          max: 5
          min: 2
      items:
        - required: true
           type: str
           format:
             strategy: from_enums
             enums:
               - ENUM_1
               - ENUM_2

The data it generates would be an array type data with size between 2 to 5 and also includes deuplicated elements:

part of result in API response
{ "data": ["ENUM_2", "ENUM_1", "ENUM_1", "ENUM_2"] }

Set a pure value element of array type property and format.unique_element as true:

api.yaml
response:
  strategy: object
  properties:
    - name: data
      required: true
      type: list
      format:
        unique_element: true
        size:
          max: 5
          min: 2
      items:
        - required: true
           type: str
           format:
             strategy: from_enums
             enums:
               - ENUM_1
               - ENUM_2

The data it generates would be an array type data with size up to 2 as maximum because the enum types only has 2 types and also includes unique elements:

part of result in API response
{ "data": ["ENUM_1", "ENUM_2"] }

The data size won't be exactly same as size setting

Sometime, the format.size setting is more than all possible values of the array type value. For example, element with enum format which only has 2 enum types, but the format.size could up to 10 as maximum size, program would ignore it and set the maximum size as the size of enum.

api.yaml
response:
  strategy: object
  properties:
    - name: data
      required: true
      type: list
      format:
        unique_element: true
        size:
          max: 100
          min: 50
      items:
        - required: true
           type: str
           format:
             strategy: from_enums
             enums:
               - ENUM_1
               - ENUM_2

The data it generates would always up to the size of enum types:

part of result in API response
{ "data": ["ENUM_1", "ENUM_2"] }

Not make sense at non-array type data

From above demonstration, beleive you could understand how to use the property format.unique_element. Therefore, it does NOT make sense about using this property with non-array type property.

api.yaml
response:
  strategy: object
  properties:
    - name: data
      required: true
      type: int  # or str, bool, etc.
      format:
        unique_element: true

New in version 0.4.2

format.enums

If the value must be one of multiple values, it could set the multiple values as an array at this property. The value would be randomly generated from the array.

Activate to use this property with strategy FROM_ENUMS.

format.customize

If the value format is complex, or it would be duplicate appeared in other multiple settings like response, it could reuse the value format setting at this property.

Activate to use this property with strategy CUSTOMIZE.

Usually be used with property format.variables or template.common_config.format

About using property format.variables, it must need to run with format setting variables. In PyFake-API-Server configuration, the format setting variables would only be set and get from 2 places: at property format.variables or in section template.common_config.format.

About the usage guide, please refer to this example.

format.variables

Set the parts of format settings as variables at this property. And the variables should be used in property format.customize.

Activate to use this property with strategy CUSTOMIZE.

About the usage guide, please refer to this example. For the details setting, please refer to here.

format.use_name

Extract the entire format settings into template.common_config.format and use this property to reuse them.

Activate to use this property with strategy FROM_TEMPLATE.

About the usage guide, please refer to this example.

Use customize

Let's easily demonstrate how to use property format.customize.

For example, we want to set a column fee.

Just randomly generate with the data type and won't configure anything:

format:
  strategy: by_data_type

This configuring way without any control. It even may response a negative value in response. If the column has some attribute like it must be a positive number, this is not a good way to configure.

  • Configure format setting to limit the value
  • Extract to configure format.variables setting into section template.common_config.format.variables for reusing
  • Extract to configure entire format setting into section template.common_config.format.entities for reusing

Limit the value should be radomly generated with the specific format and the ID must be in range 0 to 1000:

format:
  strategy: customize
  customize: amt_val USD
  variables:
    - name: amt_val
      value_format: int
      size:
        max: 1000
        min: 0

In this way, it controls the value must be XXX USD, for exmaple, it maybe 123 USD. This configurating way be more safer and more flexible to configure for generating some specific format value.

  • Configure format setting to limit the value
  • Extract to configure format.variables setting into section template.common_config.format.variables for reusing
  • Extract to configure entire format setting into section template.common_config.format.entities for reusing

In some requirement, the API would response more columns which format would be same as the id column, you have 2 choice to do:

  • Set deplicate setting in different columns
  • Extract the setting into section template and reuse it

For the first one, it's easy and won't demonstrate here. Let's demonstrate the second one: how to extract the format setting?

Set the format setting in template.common_config.format.variables configuration section:

template:
  activate: true
  common_config:
    activate: true
    format:
      variables:
        - name: amt_val
          value_format: int
          size:
            max: 1000
            min: 0

And you could clear the format setting of each columns they need in the specific API configuration as following:

format:
  strategy: customize
  customize: amt_val USD
  • Configure format setting to limit the value
  • Extract to configure format.variables setting into section template.common_config.format.variables for reusing
  • Extract to configure entire format setting into section template.common_config.format.entities for reusing

Sometimes, it has multiple columns in one APIs or multiple APIs are mostly same, it's the time to reuse entire format setting.

Set the entire format setting in template.common_config.format.entities configuration section:

template:
  activate: true
  common_config:
    activate: true
    format:
      entities:
        - name: usd_fee_value
          config:
            strategy: customize
            customize: amt_val USD
      variables:
        - name: amt_val
          value_format: int
          size:
            max: 1000
            min: 0

And you could modify the format setting of each columns they need in the specific APIs configuration as following:

format:
  strategy: from_template
  use_name: usd_fee_value
  • Configure format setting to limit the value
  • Extract to configure format.variables setting into section template.common_config.format.variables for reusing
  • Extract to configure entire format setting into section template.common_config.format.entities for reusing

format.digit

If the value is a numeric format value, you could configure this property to set the digit settings of the value.

format.digit.integer

How many digit it would have at integer part of the value.

format.digit.decimal

How many digit it would have at decimal part of the value.

Set the value digit

Configuration:

- format:
    digit:
      integer: 6
      decimal: 4

Result:

format setting

format.size

Set the size of the value. These size has 2 different meanings:

  • If value is numeric format value, the size means the maximum value and minimum value.
  • If value is string type value, the size means the maximum of value length and the minimum of value length.

And it has 2 different ways to set:

  • Set the value size from a range
  • Set the value size as a specific value

format.size.max_value

The maximum value.

format.size.min_value

The minimum value.

format.size.only_equal

If the value should be formatted as a specific size, not be randomly generated from a range, use this property to let it must be a specific size value.