You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An application's or a role's *configuration schema* is a core object that stores all
81
-
information about a user-defined configuration. To create a schema, use
82
-
the :ref:`schema.new() <config-utils-schema-new>` function. It has the following options:
80
+
A *configuration schema* stores information about a user-defined configuration structure
81
+
that can be passed inside an :ref:`app.cfg <configuration_reference_app_cfg>`
82
+
or a :ref:`roles_cfg <configuration_reference_roles_cfg>` section. It includes
83
+
option names, types, hierarchy, and other aspects of a configuration.
83
84
84
-
- schema name -- an arbitrary string
85
-
- schema node -- th
85
+
To create a schema, use the :ref:`schema.new() <config-utils-schema-new>` function.
86
+
It has the following arguments:
86
87
87
-
- scalar and composite (record, array, map) types
88
-
- annotations (type, validate, and so on)
89
-
90
-
- (optional) methods
88
+
-schema name -- an arbitrary string to use as an identifier
89
+
-schema nodes -- a hierarchical structure of configuration options that form
90
+
the schema.
91
+
- (optional) methods -- functions that can be called on this schema object.
91
92
92
93
.. _config_utils_schema_nodes:
93
94
94
95
Schema nodes
95
96
~~~~~~~~~~~~
96
97
97
-
Schema nodes can have one of two types: *scalar* or *composite*.
98
-
.. _config_utils_schema_scalar_composite_types:
99
-
100
-
Scalar and composite types
101
-
**************************
98
+
Schema nodes describe the hierarchy of options within a schema. There are two types of schema nodes:
102
99
103
-
There are scalar and composite types.
100
+
- *Scalar* nodes hold a single value of a supported primitive type. For example,
101
+
a string configuration option of a role is a scalar node its schema.
102
+
- *Composite* nodes include multiple values in different forms: records, arrays, or maps.
104
103
105
-
- Scalar type.
106
-
Can be created using ``schema.scalar()``.
107
-
There is also a shortcut: :ref:`schema.enum() <config-utils-schema-enum>`.
108
-
Learn more about supported data types: :ref:`config_utils_schema_data_types`.
109
-
- Composite data types: record, array, map.
110
104
Can be created using :ref:`schema.record() <config-utils-schema-record>`, :ref:`schema.array() <config-utils-schema-array>`, :ref:`schema.map() <config-utils-schema-map>`.
111
105
There is also a shortcut for arrays: :ref:`schema.set() <config-utils-schema-set>`.
112
106
107
+
Each schema node is defined by its *annotations*
108
+
- annotations (type, validate, and so on)
109
+
113
110
114
111
.. _config_utils_schema_nodes_scalar:
115
112
116
113
Scalar nodes
117
114
************
118
115
119
-
Scalar nodes hold a single value of a primitive type: a string, a number, a boolean value
120
-
and so on. For the full list of available scalar types, see :ref:`config_utils_schema_data_types`.
116
+
Scalar nodes hold a single value of a primitive type, for example, a string or a number.
117
+
For the full list of supported scalar types, see :ref:`config_utils_schema_data_types`.
121
118
122
119
This configuration has one scalar node of the ``string`` type:
123
120
@@ -135,18 +132,69 @@ The following code defines a configuration schema shown above:
135
132
:end-before: local function validate
136
133
:dedent:
137
134
138
-
Todo: enum?
139
-
allowed?
135
+
If a scalar node has a limited set of allowed values, you can also define it with
136
+
the :ref:`schema.enum() <config-utils-schema-enum>`. Pass the list of allowed values as
@@ -304,9 +305,12 @@ Annotations are passed as schema.scalar argument rows
304
305
305
306
Node annotations fall into three groups:
306
307
307
-
- Built-in annotations handled by the module (``validate``, ``allowed_values``, ``default``, ``apply_default_if``). Note that ``validate``, ``allowed_values`` used for validation only. ``default`` and ``apply_default_if`` can transform the configuration.
308
-
- User-defined annotations
309
-
- Computed annotations
308
+
- *Built-in annotations* are handled by the module. These are: ``type``, ``validate``, ``allowed_values``, ``default`` and ``apply_default_if``.
309
+
Note that ``validate``, ``allowed_values`` are used for validation only. ``default`` and ``apply_default_if`` can transform the configuration.
310
+
- *User-defined annotations* add named node attributes that can be used in the
311
+
application or role code.
312
+
- *Computed annotations* allow access to annotations of other nodes throughout
313
+
the schema.
310
314
311
315
.. _config_utils_schema_built_in_annotations:
312
316
@@ -318,7 +322,11 @@ TODO: check the ``Built-in annotation`` term.
318
322
Built-in annotations are interpreted by the module itself. There are the following
319
323
built-in annotations:
320
324
321
-
-
325
+
- ``type`` -- the node value type. Mandatory for scalar nodes, except those created with ``schema.enum``.
326
+
- ``allowed_values`` -- a list of possible node values.
327
+
- ``validate`` -- a validation function for the provided node value.
328
+
- ``default`` -- a value to use if the option is not specified in the configuration.
329
+
- ``apply_default_if`` -- a function that defines when to apply the default value.
322
330
323
331
Consider the following role configuration:
324
332
@@ -371,27 +379,32 @@ See the full sample here: :ref:`config_utils_schema_env-vars`.
371
379
Computed annotations
372
380
^^^^^^^^^^^^^^^^^^^^
373
381
374
-
*Computed annotations* enable access to schema data
382
+
*Computed annotations* enable access from a node to annotations of its ancestor nodes.
375
383
376
-
In the example below, the validate function of the listen_address record
377
-
uses computed annotation to access the schema data from outside the record:
384
+
In the example below, the ``listen_address`` record validation function refers to the
0 commit comments