Skip to content

Commit 405e847

Browse files
committed
Fix
1 parent e71f41e commit 405e847

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

doc/reference/reference_lua/config/utils_schema.rst

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ three scalar fields:
199199
:start-at: roles
200200
:dedent:
201201

202-
To define a record node in a schema, use :ref:`schema.record() <config-utils-schema-record>`:
202+
To define a record node in a schema, use :ref:`schema.record() <config-utils-schema-record>`.
203203
The following schema describes the configuration above:
204204

205205
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_record/http_api.lua
@@ -314,11 +314,11 @@ Built-in annotations
314314
Built-in annotations are interpreted by the module itself. There are the following
315315
built-in annotations:
316316

317-
- :ref:`type <config-schema_node_object-type>` -- the node value type. Mandatory for scalar nodes, except for those created with ``schema.enum()``.
318-
- :ref:`allowed_values <config-schema_node_object-allowed_values>` -- a list of possible node values.
319-
- :ref:`validate <config-schema_node_object-validate>` -- a validation function for the provided node value.
320-
- :ref:`default <config-schema_node_object-default>` -- a value to use if the option is not specified in the configuration.
321-
- :ref:`apply_default_if <config-schema_node_object-apply_default_if>` -- a function that defines when to apply the default value.
317+
- :ref:`type <config-schema_node_annotation-type>` -- the node value type. Mandatory for scalar nodes, except for those created with ``schema.enum()``.
318+
- :ref:`allowed_values <config-schema_node_annotation-allowed_values>` -- a list of possible node values.
319+
- :ref:`validate <config-schema_node_annotation-validate>` -- a validation function for the provided node value.
320+
- :ref:`default <config-schema_node_annotation-default>` -- a value to use if the option is not specified in the configuration.
321+
- :ref:`apply_default_if <config-schema_node_annotation-apply_default_if>` -- a function that defines when to apply the default value.
322322

323323
Consider the following role configuration:
324324

@@ -468,7 +468,7 @@ Transforming configuration
468468

469469
The module provides methods that transform configuration data based on the schema,
470470
for example, :ref:`apply_default() <config-schema_object-apply_default>`,
471-
:ref:`merge() <config-schema_object-merge>`, ref:`set() <config-schema_object-set>`.
471+
:ref:`merge() <config-schema_object-merge>`, :ref:`set() <config-schema_object-set>`.
472472

473473
The following sample shows how to apply default values from the schema to fill
474474
missing configuration fields:
@@ -485,9 +485,9 @@ missing configuration fields:
485485
Parsing environment variables
486486
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
487487

488-
The :ref:`schema.fromenv() <config-utils-schema-fromenv>` allows getting configuration
489-
values from environment variables. The example below shows how to do this by
490-
adding a user-defined annotation ``env``:
488+
The :ref:`schema.fromenv() <config-utils-schema-fromenv>` function allows getting
489+
configuration values from environment variables. The example below shows how to do
490+
this by adding a user-defined annotation ``env``:
491491

492492
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_fromenv/http_api.lua
493493
:language: lua
@@ -674,12 +674,14 @@ Functions
674674
How the raw value is parsed depends on the ``schema_node`` type:
675675

676676
- Scalar:
677+
677678
- ``string``: return the value as is
678-
- ``number`` or ``integer: parse the value as a number or an integer
679+
- ``number`` or ``integer``: parse the value as a number or an integer
679680
- ``string, number``: attempt to parse as a number; in case of a failure
680681
return the value as is
681682
- ``boolean``: accept ``true`` or ``1`` for ``true``, ``false`` or ``0`` for ``false``
682683
- ``any``: parse the value as a JSON
684+
683685
- Map: parse either as JSON (if the raw value starts with ``{``)
684686
or as a comma-separated string of ``key=value`` pairs: ``key1=value1,key2=value2``
685687
- Array: parse either as JSON (if the raw value starts with ``[``)
@@ -804,7 +806,7 @@ schema_object
804806

805807
**See also:** :ref:`config-schema_node_annotation-default`, :ref:`config-schema_node_annotation-apply_default_if`
806808

807-
.. _config-schema_object-filter:
809+
.. _config-schema_object-filter:
808810

809811
.. method:: filter(data, f)
810812

@@ -877,7 +879,7 @@ schema_object
877879

878880
- ``w.schema`` -- schema node
879881
- ``w.path`` -- the path to the schema node
880-
- ``w.error()` -- a function for printing human-readable error messages
882+
- ``w.error()`` -- a function for printing human-readable error messages
881883

882884
- ``ctx`` -- additional *context* for the transformation function. Can be
883885
used to provide values for a specific call.
@@ -1046,7 +1048,7 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10461048

10471049
- ``w.schema`` -- schema node
10481050
- ``w.path`` -- the path to the schema node
1049-
- ``w.error()` -- a function for printing human-readable error messages
1051+
- ``w.error()`` -- a function for printing human-readable error messages
10501052

10511053
**See also:** :ref:`config-schema_object-apply_default`.
10521054

@@ -1083,31 +1085,19 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10831085

10841086
- ``w.schema`` -- schema node
10851087
- ``w.path`` -- the path to the schema node
1086-
- ``w.error()` -- a function for printing human-readable error messages
1088+
- ``w.error()`` -- a function for printing human-readable error messages
10871089

10881090
See also :ref:`config-schema_object-validate`
10891091

10901092
**Example:**
10911093

1092-
.. code-block:: lua
1093-
1094-
local schema = require('experimental.config.utils.schema')
1095-
1096-
local function validate_email(email, w)
1097-
if email:find('@') == nil then
1098-
w.error('A email must contain @ symbol, got %q', email)
1099-
end
1100-
end
1101-
1102-
local personal_info_schema = schema.new('personal_info', schema.record({
1103-
email = schema.scalar({
1104-
type = 'string',
1105-
validate = validate_email,
1106-
}),
1107-
}))
1094+
A function that checks that a string a valid IP address:
11081095

1109-
personal_info_schema:validate({email = 'foo'})
1110-
-- error: [personal_info] email: A email must contain @ symbol, got "foo"
1096+
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_annotations/http_api.lua
1097+
:language: lua
1098+
:start-at: local function validate_host
1099+
:end-before: local function validate_port
1100+
:dedent:
11111101

11121102
.. _config-utils-schema_node_object:
11131103

0 commit comments

Comments
 (0)