# Recipe

Work with recipes.

## Validation

Recipes are checked against a [JSON Schema](https://json-schema.org) that
describes their structure and the arguments of all preprocessor functions.
The same schema can be used by editors to check recipes while writing them,
refer to [Recipe validation](../recipe/overview.html.md#recipe-validation) for details.

For example, to find the problems in a recipe, with their line numbers:

```pycon
>>> from esmvalcore.recipe import validate_recipe
>>> text = '''
... documentation:
...   title: Example
...   description: Example recipe.
...   authors: [andela_bouwe]
... diagnostics:
...   example:
...     scripts: null
... preprocessors:
...   pp:
...     regird: {}
... '''
>>> for issue in validate_recipe(text):
...     print(issue)
line 11: preprocessors > pp > regird: unknown preprocessor function. Did you mean 'regrid'?
```

**Classes:**

| [`RecipeIssue`](#esmvalcore.recipe.RecipeIssue)(path, message[, line, column, ...])   | A problem found in a recipe.   |
|----------------------------------------------------------------------------------------------------|--------------------------------|

**Functions:**

| [`generate_recipe_schema`](#esmvalcore.recipe.generate_recipe_schema)([session])   | Generate a JSON Schema for recipes.        |
|--------------------------------------------------------------------------------------|--------------------------------------------|
| [`validate_recipe`](#esmvalcore.recipe.validate_recipe)(text[, session])    | Validate a recipe against the JSON Schema. |

### *class* esmvalcore.recipe.RecipeIssue(path: [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[str](https://docs.python.org/3/builtins/stdtypes.html#str) | [int](https://docs.python.org/3/builtins/functions.html#int), ...], message: [str](https://docs.python.org/3/builtins/stdtypes.html#str), line: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, column: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, severity: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['error', 'warning'] = 'error')

A problem found in a recipe.

* **Parameters:**
  * **path** ([*tuple*](https://docs.python.org/3/builtins/stdtypes.html#tuple) *[*[*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *|* [*int*](https://docs.python.org/3/builtins/functions.html#int) *,*  *...* *]*) – Keys and list indices that lead to the problem in the recipe.
  * **message** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Description of the problem.
  * **line** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *|* *None*) – Line number in the recipe file, starting at 1, if known.
  * **column** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *|* *None*) – Column number in the recipe file, starting at 1, if known.
  * **severity** (*Literal* *[* *'error'* *,*  *'warning'* *]*) – Errors prevent the recipe from running, warnings do not.

**Attributes:**

| [`column`](#esmvalcore.recipe.RecipeIssue.column)   |    |
|-----------------------------------------------------------|----|
| [`line`](#esmvalcore.recipe.RecipeIssue.line)     |    |
| [`message`](#esmvalcore.recipe.RecipeIssue.message)  |    |
| [`path`](#esmvalcore.recipe.RecipeIssue.path)     |    |
| [`severity`](#esmvalcore.recipe.RecipeIssue.severity) |    |

#### column *: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)* *= None*

#### line *: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)* *= None*

#### message *: [str](https://docs.python.org/3/builtins/stdtypes.html#str)*

#### path *: [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[str](https://docs.python.org/3/builtins/stdtypes.html#str) | [int](https://docs.python.org/3/builtins/functions.html#int), ...]*

#### severity *: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['error', 'warning']* *= 'error'*

### esmvalcore.recipe.generate_recipe_schema(session: [Session](esmvalcore.config.html.md#esmvalcore.config.Session) | [None](https://docs.python.org/3/builtins/constants.html#None) = None) → JSONSchema

Generate a JSON Schema for recipes.

The schema only allows the projects in the configuration. If ESMValTool is
installed, it also lists the provenance tags and references that it
defines, refer to [References configuration file](../quickstart/configure.html.md#config-ref).

* **Parameters:**
  **session** ([*Session*](esmvalcore.config.html.md#esmvalcore.config.Session) *|* *None*) – The session that defines the projects. If not
  given, the configuration in [`esmvalcore.config.CFG`](esmvalcore.config.html.md#esmvalcore.config.CFG) is used.
* **Returns:**
  A JSON Schema (draft-07) that describes a recipe.
* **Return type:**
  JSONSchema

### esmvalcore.recipe.validate_recipe(text: [str](https://docs.python.org/3/builtins/stdtypes.html#str), session: [Session](esmvalcore.config.html.md#esmvalcore.config.Session) | [None](https://docs.python.org/3/builtins/constants.html#None) = None) → [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[RecipeIssue](#esmvalcore.recipe.RecipeIssue)]

Validate a recipe against the JSON Schema.

Also runs checks that the schema cannot express. Some combine several
parts of the recipe, for example to check that the preprocessor of each
variable group is defined in the `preprocessors` section. Others run
code, for example to check that each variable is in the CMOR tables of its
project. Keys that are defined more than once, merge keys (`<<`) that
are used more than once or merge the mapping that contains them, and
values that parsers of YAML 1.2 read differently are reported as warnings.

* **Parameters:**
  * **text** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – The YAML text of the recipe.
  * **session** ([*Session*](esmvalcore.config.html.md#esmvalcore.config.Session) *|* *None*) – The session that the recipe will be run with. Its configuration
    defines which projects, CMOR tables, required facets, and extra facets
    can be used. If not given, the configuration in
    [`esmvalcore.config.CFG`](esmvalcore.config.html.md#esmvalcore.config.CFG) is used.
* **Returns:**
  The problems found, sorted by their position in the text. The list is
  empty if the recipe is valid.
* **Return type:**
  [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[RecipeIssue](#esmvalcore.recipe.RecipeIssue)]
