Commit 62c864d
committed
config/schema: support
The motivation is to allow to assign a user-provided `validate`
annotation for an array with unique items. This is used in the next
commit to simplify config's code.
See the API description in the documentation request below.
@TarantoolBot document
Title: schema: `unique_items` annotation
Since Tarantool 3.4 the `experimental.config.utils.schema` module has
the `unique_items` annotation taken into account by the `<schema
object>:validate()` method.
The annotation can be assigned to a schema node that represents an array
of strings. An attempt to assign it to another schema node leads to an
error on the schema object creation (`schema.new()`).
The `<schema object>:validate()` method now raises an error if the
`unique_items` annotation is assigned and truthly, but the corresponding
value (an array) contains the same string two or more times.
The `schema.set()` constructor (creates an array with enumerated values
without duplicates) now assigns the `unique_items` annotation to the
created schema node instead of defining its own `validate` annotation.
The `schema.set()` constructor now allows a user to define the
`validate` annotation for the new schema node.
Example 1 (use `unique_items`):
```lua
local schema = require('experimental.config.utils.schema')
local s = schema.new('myschema', schema.array({
items = schema.scalar({type = 'string'}),
unique_items = true,
}))
s:validate({'foo', 'bar'})
-- OK
s:validate({'foo', 'foo'})
-- error: [myschema] Values should be unique, but "foo" appears at least
-- twice
```
Example 2 (use the `schema.set()` constructor):
```lua
local schema = require('experimental.config.utils.schema')
local s = schema.new('myschema', schema.set({'foo', 'bar'}))
s:validate({'foo', 'bar'})
-- OK
s:validate({'foo', 'foo'})
-- error: [myschema] Values should be unique, but "foo" appears at least
-- twice
```
Example 3 (use the `validate` annotation):
```lua
local fun = require('fun')
local schema = require('experimental.config.utils.schema')
local s = schema.new('myschema', schema.set({
'router',
'storage',
'rebalancer',
}, {
validate = function(data, w)
local kv = fun.iter(data):map(function(x) return x, true end):tomap()
if kv.rebalancer and not kv.storage then
w.error('rebalancer needs storage role enabled')
end
end,
}))
s:validate({'storage', 'rebalancer'})
-- OK
s:validate({'rebalancer'})
-- error: [myschema] rebalancer needs storage role enabled
```
Example 4 (`unique_items` on an unsupported type):
```lua
local schema = require('experimental.config.utils.schema')
local s = schema.new('myschema', schema.array({
items = schema.scalar({type = 'integer'}),
unique_items = true,
}))
-- error: [myschema] "unique_items" requires an array of strings, got an
-- array of integer items
```unique_items annotation1 parent 875b468 commit 62c864d
File tree
3 files changed
+195
-41
lines changed- changelogs/unreleased
- src/box/lua/config/utils
- test/config-luatest
3 files changed
+195
-41
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
415 | 416 | | |
416 | 417 | | |
417 | 418 | | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | 419 | | |
431 | 420 | | |
432 | 421 | | |
| |||
445 | 434 | | |
446 | 435 | | |
447 | 436 | | |
448 | | - | |
| 437 | + | |
449 | 438 | | |
450 | 439 | | |
451 | | - | |
| 440 | + | |
452 | 441 | | |
453 | 442 | | |
454 | 443 | | |
| |||
611 | 600 | | |
612 | 601 | | |
613 | 602 | | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
614 | 653 | | |
615 | 654 | | |
616 | 655 | | |
| |||
685 | 724 | | |
686 | 725 | | |
687 | 726 | | |
| 727 | + | |
688 | 728 | | |
689 | 729 | | |
690 | 730 | | |
| |||
708 | 748 | | |
709 | 749 | | |
710 | 750 | | |
| 751 | + | |
| 752 | + | |
711 | 753 | | |
712 | 754 | | |
713 | 755 | | |
| |||
1747 | 1789 | | |
1748 | 1790 | | |
1749 | 1791 | | |
1750 | | - | |
| 1792 | + | |
1751 | 1793 | | |
1752 | 1794 | | |
1753 | 1795 | | |
| |||
1806 | 1848 | | |
1807 | 1849 | | |
1808 | 1850 | | |
| 1851 | + | |
1809 | 1852 | | |
1810 | 1853 | | |
1811 | 1854 | | |
| |||
1923 | 1966 | | |
1924 | 1967 | | |
1925 | 1968 | | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
1926 | 2026 | | |
1927 | 2027 | | |
1928 | 2028 | | |
| |||
2007 | 2107 | | |
2008 | 2108 | | |
2009 | 2109 | | |
| 2110 | + | |
| 2111 | + | |
2010 | 2112 | | |
2011 | 2113 | | |
2012 | 2114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | 227 | | |
246 | 228 | | |
247 | 229 | | |
| |||
286 | 268 | | |
287 | 269 | | |
288 | 270 | | |
289 | | - | |
| 271 | + | |
290 | 272 | | |
291 | 273 | | |
292 | 274 | | |
293 | 275 | | |
294 | | - | |
| 276 | + | |
295 | 277 | | |
296 | 278 | | |
297 | 279 | | |
298 | 280 | | |
299 | 281 | | |
300 | | - | |
| 282 | + | |
301 | 283 | | |
302 | 284 | | |
303 | 285 | | |
304 | | - | |
| 286 | + | |
305 | 287 | | |
306 | | - | |
| 288 | + | |
307 | 289 | | |
308 | 290 | | |
309 | 291 | | |
310 | 292 | | |
311 | 293 | | |
312 | | - | |
| 294 | + | |
313 | 295 | | |
314 | 296 | | |
315 | 297 | | |
| |||
320 | 302 | | |
321 | 303 | | |
322 | 304 | | |
323 | | - | |
324 | 305 | | |
325 | 306 | | |
326 | 307 | | |
327 | | - | |
328 | 308 | | |
329 | 309 | | |
330 | 310 | | |
| |||
528 | 508 | | |
529 | 509 | | |
530 | 510 | | |
| 511 | + | |
531 | 512 | | |
532 | 513 | | |
533 | 514 | | |
| |||
546 | 527 | | |
547 | 528 | | |
548 | 529 | | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
549 | 577 | | |
550 | 578 | | |
551 | 579 | | |
| |||
933 | 961 | | |
934 | 962 | | |
935 | 963 | | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
936 | 983 | | |
937 | 984 | | |
938 | 985 | | |
| |||
0 commit comments