Create a Validation Definition
A Validation Definition is a fixed reference that links a Batch of data to an Expectation Suite. It can be run by itself to validate the referenced data against the associated Expectations for testing or data exploration. Multiple Validation Definitions can also be provided to a Checkpoint which, when run, executes Actions based on the Validation Results for each provided Validation Definition.
Prerequisites
- Python version 3.8 to 3.11.
- An installation of GX 1.0.
- A preconfigured Data Context.
- A preconfigured Data Source and Data Asset connected to your data.
- A preconfigured Expectation Suite populated with Expectations.
- Procedure
- Sample code
-
Import the
ValidationDefinition
class from the GX 1.0 library:Pythonfrom great_expectations.core import ValidationDefinition
-
Request a Data Context:
Pythonimport great_expectations as gx
context = gx.get_context() -
Retrieve an Expectation Suite with Expectations.
Update the value of
suite_name
in the following code with the name of your Expectation Suite. Then execute the code to retrieve that Expectation Suite:Pythonsuite_name = "<NAME OF AN EXISTING EXPECTATION SUITE>"
suite = context.get_expectation_suite(suite_name) -
Retrieve the Batch Definition that describes the data to associate with the Expectation Suite.
Update the values of
data_source_name
,data_asset_name
, andbatch_definition_name
in the following code with the names of your previously defined Data Source, one of its Data Assets, and a Batch Definition for that Data Asset. Then execute the code to retrieve the Batch Definition:Pythondata_source_name = "my_datasource"
data_asset_name = "my_data_asset"
batch_definition_name = "my_batch_definition"
batch_definition = context.get_datasource(data_source_name).get(data_asset_name).get(batch_definition_name) -
Create a
ValidationDefinition
instance using the Batch Definition, Expectation Suite, and a unique name.Update the value of
definition_name
with a descriptive name that indicates the purpose of the Validation Definition. Then execute the code to create your Validation Definition:Pythondefinition_name = "My Validation Definition"
validation_definition = ValidationDefinition(data=batch_definition, suite=suite, name=definition_name) -
Optional. Save the Validation Definition to your Data Context.
Pythonvalidation_definition = context.validation_definitions.add(validation_definition)
tipYou can add a Validation Definition to your Data Context at the same time as you create it with the following code:
Pythondefinition_name = "My second Validation Definition"
validation_definition = context.validation_definitions.add(ValidationDefinition(data=batch_definition, suite=suite, name=definition_name))
from great_expectations.core import ValidationDefinition
import great_expectations as gx
context = gx.get_context()
suite_name = "my_expectation_suite"
suite = context.suites.get(name=suite_name)
data_source_name = "my_datasource"
data_asset_name = "my_data_asset"
batch_definition_name = "my_batch_definition"
batch_definition = context.get_datasource(data_source_name).get(data_asset_name).get(batch_definition_name)
definition_name = "My Validation Definition"
validation_definition = ValidationDefinition(data=batch_definition, suite=suite, name=definition_name)
validation_definition = context.validation_definitions.add(validation_definition)
new_definition_name = "My second Validation Definition"
validation_definition = context.validation_definitions.add(ValidationDefinition(data=batch_definition, suite=suite, name=new_definition_name))