1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 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 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Kwalify User's Guide (for Ruby)</title>
<meta name="author" content="makoto kuwata <kwa(at)kuwata-lab.com>">
<meta name="generator" content="kwaser">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="docstyle.css" type="text/css">
</head>
<body>
<div class="mainbody">
<div align="left"><h1>Kwalify User's Guide (for Ruby)</h1></div>
<div align="left">
makoto kuwata <kwa(at)kuwata-lab.com><br>
last update: $Date$<br>
</div>
<a name="preface"></a>
<h2 class="section1">Preface</h2>
<p>Kwalify<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> is a parser, schema validator, and data binding tool for YAML and JSON.
Kwalify enables you to handle YAML and JSON more easily and strictly.
</p>
<p>Topics:
</p>
<ul type="disc">
<li><a href="#schema">Schema validation for YAML</a> and <a href="#tips-json">JSON</a>
</li>
<li><a href="#actions">Class definition generation for Ruby, PHP, and Java</a>
</li>
<li><a href="#howto-databinding">Data binding</a>
</li>
<li><a href="#howto-preceding">Preceding alias</a>
</li>
</ul>
<div class="footnote">
<dl compact>
<dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
<dd>Pronounce as 'Qualify'.</dd>
</dl>
</div>
<a name="toc"></a>
<h3 class="section2">Table of Contents</h3>
<ul>
<li><a href="#preface">Preface</a>
<ul>
<li><a href="#toc">Table of Contents</a>
</li>
</ul>
</li>
<li><a href="#schema">Schema Definition</a>
<ul>
<li><a href="#schema-seq">Sequence</a>
</li>
<li><a href="#schema-map">Mapping</a>
</li>
<li><a href="#schema-seq-of-map">Sequence of Mapping</a>
</li>
<li><a href="#schema-map-of-seq">Mapping of Sequence</a>
</li>
<li><a href="#schema-rules">Rule and Constraint</a>
</li>
<li><a href="#schema-unique">Unique constraint</a>
</li>
</ul>
</li>
<li><a href="#tips">Tips</a>
<ul>
<li><a href="#tips-json">JSON</a>
</li>
<li><a href="#tips-anchor">Anchor and Alias</a>
</li>
<li><a href="#tips-default">Default of Mapping</a>
</li>
<li><a href="#tips-merge">Merging Mappings</a>
</li>
</ul>
</li>
<li><a href="#howto">How to in Ruby</a>
<ul>
<li><a href="#howot-validate">Validation</a>
</li>
<li><a href="#howto-parse">Parsing with Validation</a>
</li>
<li><a href="#howto-meta">Meta Validation</a>
</li>
<li><a href="#howto-hook">Validator#validator_hook()</a>
</li>
<li><a href="#howto-preceding">Preceding Alias</a>
</li>
<li><a href="#howto-databinding">Data Binding</a>
</li>
</ul>
</li>
<li><a href="#actions">Actions</a>
<ul>
<li><a href="#action-genclass">Class Definition Generation</a>
<ul>
<li><a href="#action-genclass-ruby">Ruby Class Definition</a>
</li>
<li><a href="#action-genclass-java">Java Class Definition</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#ref">References</a>
<ul>
<li><a href="#ref-usage">Usage in Command-Line</a>
</li>
</ul>
</li>
</ul>
<br>
<br>
<a name="schema"></a>
<h2 class="section1">Schema Definition</h2>
<p>This section describes how to define schema definition of YAML.
</p>
<a name="schema-seq"></a>
<h3 class="section2">Sequence</h3>
<a name="schema01.yaml"></a>
<div class="program_caption">
<code>schema01.yaml</code> : sequence of string</div>
<pre class="program">type: seq
sequence:
- type: str
</pre>
<a name="document01a.yaml"></a>
<div class="program_caption">
<code>document01a.yaml</code> : valid document example</div>
<pre class="program">- foo
- bar
- baz
</pre>
<a name="valid01.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema01.yaml document01a.yaml
document01a.yaml#0: valid.
</pre>
<a name="document01b.yaml"></a>
<div class="program_caption">
<code>document01b.yaml</code> : invalid document example</div>
<pre class="program">- foo
- 123
- baz
</pre>
<a name="invalid01.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema01.yaml document01b.yaml
document01b.yaml#0: INVALID
- (line 2) [/1] '123': not a string.
</pre>
<p>Default '<code>type:</code>' is <code>str</code> so you can omit '<code>type: str</code>'.
</p>
<br>
<a name="schema-map"></a>
<h3 class="section2">Mapping</h3>
<a name="schema02.yaml"></a>
<div class="program_caption">
<code>schema02.yaml</code> : mapping of scalar</div>
<pre class="program">type: map
mapping:
"name":
type: str
required: yes
"email":
type: str
pattern: /@/
"age":
type: int
"birth":
type: date
</pre>
<a name="document02a.yaml"></a>
<div class="program_caption">
<code>document02a.yaml</code> : valid document example</div>
<pre class="program">name: foo
email: foo@mail.com
age: 20
birth: 1985-01-01
</pre>
<a name="valid02.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema02.yaml document02a.yaml
document02a.yaml#0: valid.
</pre>
<a name="document02b.yaml"></a>
<div class="program_caption">
<code>document02b.yaml</code> : invalid document example</div>
<pre class="program">name: foo
email: foo(at)mail.com
age: twenty
birth: Jun 01, 1985
</pre>
<a name="invalid02.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema02.yaml document02b.yaml
document02b.yaml#0: INVALID
- (line 2) [/email] 'foo(at)mail.com': not matched to pattern /@/.
- (line 3) [/age] 'twenty': not a integer.
- (line 4) [/birth] 'Jun 01, 1985': not a date.
</pre>
<br>
<a name="schema-seq-of-map"></a>
<h3 class="section2">Sequence of Mapping</h3>
<a name="schema03.yaml"></a>
<div class="program_caption">
<code>schema03.yaml</code> : sequence of mapping</div>
<pre class="program">type: seq
sequence:
- type: map
mapping:
"name":
type: str
required: true
"email":
type: str
</pre>
<a name="document03a.yaml"></a>
<div class="program_caption">
<code>document03a.yaml</code> : valid document example</div>
<pre class="program">- name: foo
email: foo@mail.com
- name: bar
email: bar@mail.net
- name: baz
email: baz@mail.org
</pre>
<a name="valid03.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema03.yaml document03a.yaml
document03a.yaml#0: valid.
</pre>
<a name="document03b.yaml"></a>
<div class="program_caption">
<code>document03b.yaml</code> : invalid document example</div>
<pre class="program">- name: foo
email: foo@mail.com
- naem: bar
email: bar@mail.net
- name: baz
mail: baz@mail.org
</pre>
<a name="invalid03.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema03.yaml document03b.yaml
document03b.yaml#0: INVALID
- (line 3) [/1] key 'name:' is required.
- (line 3) [/1/naem] key 'naem:' is undefined.
- (line 6) [/2/mail] key 'mail:' is undefined.
</pre>
<br>
<a name="schema-map-of-seq"></a>
<h3 class="section2">Mapping of Sequence</h3>
<a name="schema04.yaml"></a>
<div class="program_caption">
<code>schema04.yaml</code> : mapping of sequence of mapping</div>
<pre class="program">type: map
mapping:
"company":
type: str
required: yes
"email":
type: str
"employees":
type: seq
sequence:
- type: map
mapping:
"code":
type: int
required: yes
"name":
type: str
required: yes
"email":
type: str
</pre>
<a name="document04a.yaml"></a>
<div class="program_caption">
<code>document04a.yaml</code> : valid document example</div>
<pre class="program">company: Kuwata lab.
email: webmaster@kuwata-lab.com
employees:
- code: 101
name: foo
email: foo@kuwata-lab.com
- code: 102
name: bar
email: bar@kuwata-lab.com
</pre>
<a name="valid04.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema04.yaml document04a.yaml
document04a.yaml#0: valid.
</pre>
<a name="document04b.yaml"></a>
<div class="program_caption">
<code>document04b.yaml</code> : invalid document example</div>
<pre class="program">company: Kuwata Lab.
email: webmaster@kuwata-lab.com
employees:
- code: A101
name: foo
email: foo@kuwata-lab.com
- code: 102
name: bar
mail: bar@kuwata-lab.com
</pre>
<a name="invalid04.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema04.yaml document04b.yaml
document04b.yaml#0: INVALID
- (line 4) [/employees/0/code] 'A101': not a integer.
- (line 9) [/employees/1/mail] key 'mail:' is undefined.
</pre>
<br>
<a name="schema-rules"></a>
<h3 class="section2">Rule and Constraint</h3>
<p><code>type:</code>, <code>required:</code>, <code>length</code>, ... are called <strong>constraint</strong> and set of constraints are called <strong>rule</strong>.
</p>
<ul type="disc">
<li>Rule contains 'type:' constraint. If 'type:' is omitted, 'type: str' is used as default.
</li>
<li>'sequence:' constraint takes a sequence of rule (the sequence can contain only a rule).
</li>
<li>'mapping:' constraint takes a mapping which values are rules.
</li>
</ul>
<div style="align:center;">
<img src="img/fig01.png" alt="constraints and rules of schema definition." />
</div>
<p>The following is a list of constraints.
</p>
<dl class="dl3">
<dt class="dt3"><strong>
<code>required:</code> </strong></dt>
<dd class="dd3">
Value is required when true (default is false).
This is similar to not-null constraint in RDBMS.
</dd>
<dt class="dt3"><strong>
<code>enum:</code> </strong></dt>
<dd class="dd3">
List of available values.
</dd>
<dt class="dt3"><strong>
<code>pattern:</code> </strong></dt>
<dd class="dd3">
Specifies regular expression pattern of value.
</dd>
<dt class="dt3"><strong>
<code>type:</code> </strong></dt>
<dd class="dd3">
Type of value. The followings are available:
<ul type="circle">
<li><code>str</code>
</li>
<li><code>int</code>
</li>
<li><code>float</code>
</li>
<li><code>number</code> (== int or float)
</li>
<li><code>text</code> (== str or number)
</li>
<li><code>bool</code>
</li>
<li><code>date</code>
</li>
<li><code>time</code>
</li>
<li><code>timestamp</code>
</li>
<li><code>seq</code>
</li>
<li><code>map</code>
</li>
<li><code>scalar</code> (all but seq and map)
</li>
<li><code>any</code> (means any data)
</li>
</ul>
</dd>
<dt class="dt3"><strong>
<code>range:</code> </strong></dt>
<dd class="dd3">
Range of value between max/max-ex and min/min-ex.
<ul type="circle">
<li>'max' means 'max-inclusive'.
</li>
<li>'min' means 'min-inclusive'.
</li>
<li>'max-ex' means 'max-exclusive'.
</li>
<li>'min-ex' means 'min-exclusive'.
</li>
</ul>
Type <code>seq</code>, <code>map</code>, <code>bool</code> and <code>any</code> are not available with <code>range:</code>.
</dd>
<dt class="dt3"><strong>
<code>length:</code> </strong></dt>
<dd class="dd3">
Range of length of value between max/max-ex and min/min-ex.
Only type <code>str</code> and <code>text</code> are available with <code>length:</code>.
</dd>
<dt class="dt3"><strong>
<code>assert:</code> </strong></dt>
<dd class="dd3">
String which represents validation expression.
String should contain variable name <code>val</code> which repsents value.
(This is an experimental function and not supported in Kwartz-java).
</dd>
<dt class="dt3"><strong>
<code>unique:</code> </strong></dt>
<dd class="dd3">
Value is unique for mapping or sequence.
This is similar to unique constraint of RDBMS.
See the next subsection for detail.
</dd>
<dt class="dt3"><strong>
<code>name:</code> </strong></dt>
<dd class="dd3">
Name of schema.
</dd>
<dt class="dt3"><strong>
<code>desc:</code> </strong></dt>
<dd class="dd3">
Description. This is not used for validation.
</dd>
<dt class="dt3"><strong>
<code>class:</code> </strong></dt>
<dd class="dd3">
Class name. This is for data-binding and is available only with type 'map'.
This is also used in 'genclass' action.
</dd>
<dt class="dt3"><strong>
<code>default:</code> </strong></dt>
<dd class="dd3">
Default value.
This is only for 'genclass' action, and have no effect to validation and parsing.
Default value should be scalar and it is not available if <code>type:</code> is <code>map</code> or <code>seq</code>, and also not available when <code>required:</code> is true.
</dd>
</dl>
<a name="schema05.yaml"></a>
<div class="program_caption">
<code>schema05.yaml</code> : rule examples</div>
<pre class="program">type: seq
sequence:
-
type: map
mapping:
"name":
type: str
required: yes
"email":
type: str
required: yes
pattern: /@/
"password":
type: text
length: { max: 16, min: 8 }
"age":
type: int
range: { max: 30, min: 18 }
# or assert: 18 <= val && val <= 30
"blood":
type: str
enum: [A, B, O, AB]
"birth":
type: date
"memo":
type: any
"deleted":
type: bool
default: false
</pre>
<a name="document05a.yaml"></a>
<div class="program_caption">
<code>document05a.yaml</code> : valid document example</div>
<pre class="program">- name: foo
email: foo@mail.com
password: xxx123456
age: 20
blood: A
birth: 1985-01-01
- name: bar
email: bar@mail.net
age: 25
blood: AB
birth: 1980-01-01
</pre>
<a name="valid05.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema05.yaml document05a.yaml
document05a.yaml#0: valid.
</pre>
<a name="document05b.yaml"></a>
<div class="program_caption">
<code>document05b.yaml</code> : invalid document example</div>
<pre class="program">- name: foo
email: foo(at)mail.com
password: xxx123
age: twenty
blood: a
birth: 1985-01-01
- given-name: bar
family-name: Bar
email: bar@mail.net
age: 15
blood: AB
birth: 1980/01/01
</pre>
<a name="invalid05.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema05.yaml document05b.yaml
document05b.yaml#0: INVALID
- (line 2) [/0/email] 'foo(at)mail.com': not matched to pattern /@/.
- (line 3) [/0/password] 'xxx123': too short (length 6 < min 8).
- (line 4) [/0/age] 'twenty': not a integer.
- (line 5) [/0/blood] 'a': invalid blood value.
- (line 7) [/1] key 'name:' is required.
- (line 7) [/1/given-name] key 'given-name:' is undefined.
- (line 8) [/1/family-name] key 'family-name:' is undefined.
- (line 10) [/1/age] '15': too small (< min 18).
- (line 12) [/1/birth] '1980/01/01': not a date.
</pre>
<br>
<a name="schema-unique"></a>
<h3 class="section2">Unique constraint</h3>
<p>'<code>unique:</code>' constraint is available with elements of sequence or mapping.
This is equivalent to unique constraint of RDBMS.
</p>
<ul type="disc">
<li>Type of rule which has '<code>unique:</code>' entry must be scalar (str, int, float, ...).
</li>
<li>Type of parent rule must be sequence or mapping.
</li>
</ul>
<a name="schema06.yaml"></a>
<div class="program_caption">
<code>schema06.yaml</code> : unique constraint entry with mapping and sequence</div>
<pre class="program">type: seq
sequence:
- type: map
required: yes
mapping:
"name": { type: str, required: yes, <strong>unique: yes</strong> }
"email": { type: str }
"groups":
type: seq
sequence:
- { type: str, <strong>unique: yes</strong> }
</pre>
<a name="document06a.yaml"></a>
<div class="program_caption">
<code>document06a.yaml</code> : valid document example</div>
<pre class="program">- name: foo
email: admin@mail.com
groups:
- users
- foo
- admin
- name: bar
email: admin@mail.com
groups:
- users
- admin
- name: baz
email: baz@mail.com
groups:
- users
</pre>
<a name="valid06.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema06.yaml document06a.yaml
document06a.yaml#0: valid.
</pre>
<a name="document06b.yaml"></a>
<div class="program_caption">
<code>document06b.yaml</code> : invalid document example</div>
<pre class="program">- name: foo
email: admin@mail.com
groups:
- foo
- users
- admin
- foo
- name: bar
email: admin@mail.com
groups:
- admin
- users
- name: bar
email: baz@mail.com
groups:
- users
</pre>
<a name="invalid06.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema06.yaml document06b.yaml
document06b.yaml#0: INVALID
- (line 7) [/0/groups/3] 'foo': is already used at '/0/groups/0'.
- (line 13) [/2/name] 'bar': is already used at '/1/name'.
</pre>
<br>
<br>
<a name="tips"></a>
<h2 class="section1">Tips</h2>
<a name="tips-json"></a>
<h3 class="section2">JSON</h3>
<p><a href="http://www.json.org">JSON</a> is a lightweight data-interchange format, especially useful for JavaScript.
JSON can be considered as a subset of YAML. It means that YAML parser can parse JSON and Kwalify can validate JSON document.
</p>
<a name="schema12.json"></a>
<div class="program_caption">
<code>schema12.json</code> : an example schema definition written in JSON format</div>
<pre class="program">{ "type": "map",
"required": true,
"mapping": {
"name": { "type": "str", "required": true },
"email": { "type": "str" },
"age": { "type": "int" },
"gender": { "type": "str", "enum": ["M", "F"] },
"favorite": { "type": "seq",
"sequence": [ { "type": "str" } ]
}
}
}
</pre>
<a name="document12a.json"></a>
<div class="program_caption">
<code>document12a.json</code> : valid JSON document example</div>
<pre class="program">{ "name": "Foo",
"email": "foo@mail.com",
"age": 20,
"gender": "F",
"favorite": [
"football",
"basketball",
"baseball"
]
}
</pre>
<a name="valid12.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema12.json document12a.json
document12a.json#0: valid.
</pre>
<a name="document12b.json"></a>
<div class="program_caption">
<code>document12b.json</code> : invalid JSON document example</div>
<pre class="program">{
"mail": "foo@mail.com",
"age": twenty,
"gender": "X",
"favorite": [ 123, 456 ]
}
</pre>
<a name="invalid12.json"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema12.json document12b.json
document12b.json#0: INVALID
- (line 1) [/] key 'name:' is required.
- (line 2) [/mail] key 'mail:' is undefined.
- (line 3) [/age] 'twenty': not a integer.
- (line 4) [/gender] 'X': invalid gender value.
- (line 5) [/favorite/0] '123': not a string.
- (line 5) [/favorite/1] '456': not a string.
</pre>
<br>
<a name="tips-anchor"></a>
<h3 class="section2">Anchor and Alias</h3>
<p>You can share rules by YAML anchor and alias.
</p>
<a name="schema13.yaml"></a>
<div class="program_caption">
<code>schema13.yaml</code> : anchor example</div>
<pre class="program">type: seq
sequence:
- <strong>&employee</strong>
type: map
mapping:
"given-name": <strong>&name</strong>
type: str
required: yes
"family-name": <strong>*name</strong>
"post":
type: str
enum: [exective, manager, clerk]
"supervisor": <strong>*employee</strong>
</pre>
<p>Anchor and alias is also available in YAML document.
</p>
<a name="document13a.yaml"></a>
<div class="program_caption">
<code>document13a.yaml</code> : valid document example</div>
<pre class="program">- <strong>&foo</strong>
given-name: foo
family-name: Foo
post: exective
- <strong>&bar</strong>
given-name: bar
family-name: Bar
post: manager
supervisor: <strong>*foo</strong>
- given-name: baz
family-name: Baz
post: clerk
supervisor: <strong>*bar</strong>
- given-name: zak
family-name: Zak
post: clerk
supervisor: <strong>*bar</strong>
</pre>
<a name="valid13.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema13.yaml document13a.yaml
document13a.yaml#0: valid.
</pre>
<br>
<a name="tips-default"></a>
<h3 class="section2">Default of Mapping</h3>
<p>YAML allows user to specify default value of mapping.
</p>
<p>For example, the following YAML document uses default value of mapping.
</p>
<pre class="program">A: 10
B: 20
<strong>=: -1</strong> # default value
</pre>
<p>This is equal to the following Ruby code.
</p>
<pre class="program">map = ["A"=>10, "B"=>20]
map.default = -1
map
</pre>
<p>Kwalify allows user to specify default rule using default value of mapping.
It is useful when key names are unknown.
</p>
<a name="schema14.yaml"></a>
<div class="program_caption">
<code>schema14.yaml</code> : default rule example</div>
<pre class="program">type: map
mapping:
<strong>=:</strong> # default rule
type: number
range: { max: 1, min: -1 }
</pre>
<a name="document14a.yaml"></a>
<div class="program_caption">
<code>document14a.yaml</code> : valid document example</div>
<pre class="program">value1: 0
value2: 0.5
value3: -0.9
</pre>
<a name="valid14.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema14.yaml document14a.yaml
document14a.yaml#0: valid.
</pre>
<a name="document14b.yaml"></a>
<div class="program_caption">
<code>document14b.yaml</code> : invalid document example</div>
<pre class="program">value1: 0
value2: 1.1
value3: -2.0
</pre>
<a name="invalid14.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema14.yaml document14b.yaml
document14b.yaml#0: INVALID
- (line 2) [/value2] '1.1': too large (> max 1).
- (line 3) [/value3] '-2.0': too small (< min -1).
</pre>
<br>
<a name="tips-merge"></a>
<h3 class="section2">Merging Mappings</h3>
<p>YAML allows user to merge mappings.
</p>
<pre class="program">- <strong>&a1</strong>
A: 10
B: 20
- <strong><<: *a1</strong> # merge
A: 15 # override
C: 30 # add
</pre>
<p>This is equal to the following Ruby code.
</p>
<pre class="program">a1 = {"A"=>10, "B"=>20}
tmp = {}
tmp.update(a1) # merge
tmp["A"] = 15 # override
tmp["C"] = 30 # add
</pre>
<p>This feature allows Kwalify to merge rule entries.
</p>
<a name="schema15.yaml"></a>
<div class="program_caption">
<code>schema15.yaml</code> : merging rule entries example</div>
<pre class="program">type: map
mapping:
"group":
type: map
mapping:
"name": <strong>&name</strong>
type: str
required: yes
"email": <strong>&email</strong>
type: str
pattern: /@/
required: no
"user":
type: map
mapping:
"name":
<strong><<: *name</strong> # merge
<strong>length: { max: 16 }</strong> # add
"email":
<strong><<: *email</strong> # merge
<strong>required: yes</strong> # override
</pre>
<a name="document15a.yaml"></a>
<div class="program_caption">
<code>document15a.yaml</code> : valid document example</div>
<pre class="program">group:
name: foo
email: foo@mail.com
user:
name: bar
email: bar@mail.com
</pre>
<a name="valid15.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema15.yaml document15a.yaml
document15a.yaml#0: valid.
</pre>
<a name="document15b.yaml"></a>
<div class="program_caption">
<code>document15b.yaml</code> : invalid document example</div>
<pre class="program">group:
name: foo
email: foo@mail.com
user:
name: toooooo-looooong-name
</pre>
<a name="invalid15.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ kwalify -lf schema15.yaml document15b.yaml
document15b.yaml#0: INVALID
- (line 5) [/user] key 'email:' is required.
- (line 5) [/user/name] 'toooooo-looooong-name': too long (length 21 > max 16).
</pre>
<br>
<br>
<a name="howto"></a>
<h2 class="section1">How to in Ruby</h2>
<p>This section describes how to use Kwalify in Ruby.
</p>
<a name="howot-validate"></a>
<h3 class="section2">Validation</h3>
<a name="howto-validation.rb"></a>
<pre class="program">require 'kwalify'
#require 'yaml'
## load schema data
schema = Kwalify::Yaml.load_file('schema.yaml')
## or
#schema = YAML.load_file('schema.yaml')
## create validator
validator = Kwalify::Validator.new(schema)
## load document
document = Kwalify::Yaml.load_file('document.yaml')
## or
#document = YAML.load_file('document.yaml')
## validate
errors = validator.validate(document)
## show errors
if errors && !errors.empty?
for e in errors
puts "[#{e.path}] #{e.message}"
end
end
</pre>
<br>
<a name="howto-parse"></a>
<h3 class="section2">Parsing with Validation</h3>
<p>From version 0.7, Kwalify supports parsing with validation.
</p>
<a name="howto-validation-with-parsing.rb"></a>
<pre class="program">require 'kwalify'
#require 'yaml'
## load schema data
schema = Kwalify::Yaml.load_file('schema.yaml')
## or
#schema = YAML.load_file('schema.yaml')
## create validator
validator = Kwalify::Validator.new(schema)
## create parser with validator
## (if validator is ommitted, no validation executed.)
parser = Kwalify:::Yaml::Parser.new(validator)
## parse document with validation
filename = 'document.yaml'
document = parser.parse_file(filename)
## or
#document = parser.parse(File.read(filename), filename)
## show errors if exist
errors = parser.errors()
if errors && !errors.empty?
for e in errors
puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
end
end
</pre>
<br>
<a name="howto-meta"></a>
<h3 class="section2">Meta Validation</h3>
<p>Meta validator is a validator which validates schema definition.
The schema definition is placed at 'kwalify/kwalify.schema.yaml'.
</p>
<p>Kwalify also provides Kwalify::MetaValidator class which validates
schema defition.
</p>
<pre class="program">require 'kwalify'
## meta validator
metavalidator = Kwalify::MetaValidator.instance
## validate schema definition
parser = Kwalify::Yaml::Parser.new(metavalidator)
errors = parser.parse_file('schema.yaml')
for e in errors
puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
end if errors && !errors.empty?
</pre>
<p>Meta validation is also available with command-line option '-m'.
</p>
<pre class="terminal">$ kwalify -m schema1.yaml schema2.yaml ...
</pre>
<br>
<a name="howto-hook"></a>
<h3 class="section2">Validator#validator_hook()</h3>
<p>You can extend Kwalify::Validator and override Kwalify::Validator#validator_hook() method.
This method is called by Kwalify::Validator#validate().
</p>
<a name="answers-schema.yaml"></a>
<div class="program_caption">
answers-schema.yaml : 'name:' is important.</div>
<pre class="program">type: map
mapping:
"answers":
type: seq
sequence:
- type: map
<strong>name: Answer</strong>
mapping:
"name": { type: str, required: yes }
"answer": { type: str, required: yes,
enum: [good, not bad, bad] }
"reason": { type: str }
</pre>
<a name="answers-validator.rb"></a>
<div class="program_caption">
answers-validator.rb : validate script for Ruby</div>
<pre class="program">#!/usr/bin/env ruby
require 'kwalify'
## validator class for answers
class AnswersValidator < Kwalify::Validator
## load schema definition
@@schema = Kwalify::Yaml.load_file('answers-schema.yaml')
## or
## require 'yaml'
## @@schema = YAML.load_file('answers-schema.yaml')
def initialize()
super(@@schema)
end
## hook method called by Validator#validate()
<strong>def validate_hook(value, rule, path, errors)</strong>
<strong>case rule.name</strong>
<strong>when 'Answer'</strong>
if value['answer'] == 'bad'
reason = value['reason']
if !reason || reason.empty?
msg = "reason is required when answer is 'bad'."
errors << Kwalify::ValidationError.new(msg, path)
end
end
end
end
end
## create validator
validator = AnswersValidator.new
## parse and validate YAML document
input = ARGF.read()
parser = Kwalify::Yaml::Parser.new(validator)
document = parser.parse(input)
## show errors
errors = parser.errors()
if !errors || errors.empty?
puts "Valid."
else
puts "*** INVALID!"
for e in errors
# e.class == Kwalify::ValidationError
puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
end
end
</pre>
<a name="document07a.yaml"></a>
<div class="program_caption">
<code>document07a.yaml</code> : valid document example</div>
<pre class="program">answers:
- name: Foo
answer: good
reason: I like this style.
- name: Bar
answer: not bad
- name: Baz
answer: bad
reason: I don't like this style.
</pre>
<a name="valid07.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ ruby answers-validator.rb document07a.yaml
Valid.
</pre>
<a name="document07b.yaml"></a>
<div class="program_caption">
<code>document07b.yaml</code> : invalid document example</div>
<pre class="program">answers:
- name: Foo
answer: good
- name: Bar
answer: bad
- name: Baz
answer: not bad
</pre>
<a name="invalid07.result"></a>
<div class="terminal_caption">
validate</div>
<pre class="terminal">$ ruby answers-validator.rb document07b.yaml
*** INVALID!
4:3 [/answers/1] reason is required when answer is 'bad'.
</pre>
<p>You can validate some document by a Validator instance because Validator class and Validator#validate() method are stateless. If you use instance variables in custom validator_hook() method, it becomes to be stateful.
</p>
<br>
<a name="howto-preceding"></a>
<h3 class="section2">Preceding Alias</h3>
<p>From version 0.7, Kwalify allows aliases to appear before corresponding anchors are now appeared.
These aliases are called as 'preceding alias'.
</p>
<a name="howto3.yaml"></a>
<div class="program_caption">
howto3.yaml</div>
<pre class="program">- name: Foo
parent: *bar # preceding alias
- &bar
name: Bar
parent: *baz # preceding alias
- &baz
name: Baz
parent: null
</pre>
<p>To enable preceding alias, set Kwalify::Yaml::Parser#preceding_alias to true.
</p>
<a name="howto3.rb"></a>
<div class="program_caption">
howto3.rb</div>
<pre class="program">require 'kwalify'
parser = Kwalify::Yaml::Parser.new
<strong>parser.preceding_alias = true</strong> # enable preceding alias
ydoc = parser.parse_file('howto3.yaml')
require 'pp'
pp ydoc
</pre>
<a name="howto3.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ ruby howto3.rb
[{"name"=>"Foo",
"parent"=>{"name"=>"Bar", "parent"=>{"name"=>"Baz", "parent"=>nil}}},
{"name"=>"Bar", "parent"=>{"name"=>"Baz", "parent"=>nil}},
{"name"=>"Baz", "parent"=>nil}]
</pre>
<p>Command-line option '-P' also enables preceding alias.
</p>
<p>Preceding alias is very useful when document is complex.
</p>
<br>
<a name="howto-databinding"></a>
<h3 class="section2">Data Binding</h3>
<p>From version 0.7, Kwalify supports data binding.
* To enable data binding, set Kwlaify::Yaml::Parser#data_binding to true.
* It is required to specify class name in schema definition.
(Notice that 'class:' constraint is avaialbe only with rule which type is 'map'.)
* Also instance methods '[]', '[]=', and 'keys?' must be defined in the classes.
(Including Kwalify::Util::HashLike modules is easy way to define them.)
</p>
<a name="config.schema.yaml"></a>
<div class="program_caption">
config.schema.yaml: schema definition file</div>
<pre class="program">type: map
<strong>class: Config</strong>
mapping:
"host": { type: str, required: true }
"port": { type: int }
"user": { type: str, required: true }
"pass": { type: str, required: true }
</pre>
<a name="config.yaml"></a>
<div class="program_caption">
config.yaml: data file</div>
<pre class="program">host: localhost
port: 8080
user: user1
pass: password1
</pre>
<a name="loadconfig.rb"></a>
<div class="program_caption">
loadconfig.rb: ruby program</div>
<pre class="program">## class definition
require 'kwalify/util/hashlike'
<strong>class Config</strong>
<strong>include Kwalify::Util::HashLike</strong> # defines [], []=, and keys?
attr_accessor :host, :posrt, :user, :pass
<strong>end</strong>
## create validator object
require 'kwalify'
schema = Kwalify::Yaml.load_file('config.schema.yaml')
validator = Kwalify::Validator.new(schema)
## parse configuration file with data binding
parser = Kwalify::Yaml::Parser.new(validator)
<strong>parser.data_binding = true</strong> # enable data binding
config = parser.parse_file('config.yaml')
require 'pp'
pp config
</pre>
<a name="loadconfig.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ ruby loadconfig.rb
#<Config:
@host="localhost",
@pass="password1",
@port=8080,
@user="user1">
</pre>
<p>Data binding is available even when data is more complex.
Preceding alias is also available.
</p>
<p>For example, the following data is complex because it uses anchor and alias (including preceding alias).
</p>
<a name="BABEL.data.yaml"></a>
<div class="program_caption">
BABEL.data.yaml</div>
<pre class="program">teams:
- &thechildren
name: The Children
desc: Level 7 ESPers
chief: *minamoto # preceding alias
members: [*kaoru, *aoi, *shiho] # preceding aliases
members:
- &minamoto
name: Kohichi Minamoto
desc: Scientist
team: *thechildren
- &kaoru
name: Kaoru Akashi
desc: Psychokino
team: *thechildren
- &aoi
name: Aoi Nogami
desc: Teleporter
team: *thechildren
- &shiho
name: Shiho Sannomiya
desc: Psycometrer
team: *thechildren
</pre>
<p>Here is the schema definition.
(Notice that 'class:' constraint is avaialbe only with rule which type is 'map'.)
</p>
<a name="BABEL.schema.yaml"></a>
<div class="program_caption">
BABEL.schema.yaml</div>
<pre class="program">type: map
required: yes
mapping:
"teams":
type: seq
required: yes
sequence:
- &team
type: map
required: yes
<strong>class: Team</strong>
mapping:
"name": {type: str, required: yes, unique: yes}
"desc": {type: str}
"chief": *member # preceding alias
"members":
type: seq
sequence: [*member] # preceding alias
"members":
type: seq
required: yes
sequence:
- &member
type: map
required: yes
<strong>class: Member</strong>
mapping:
"name": {type: str, required: yes, unique: yes}
"desc": {type: str}
"team": *team
</pre>
<p>It is required to define class 'Team' and 'Member' for data-binding.
Command-line option '-a genclass-ruby' will help you to generate class definitions from schema definition.
Try 'kwalify -ha genclass-ruby' for more details about 'genclass-ruby' action.
</p>
<a name="babel_genclass.result"></a>
<pre class="terminal">$ kwalify -a genclass-ruby -P -f BABEL.schema.yaml \
--hashlike --initialize=false --module=Babel
require 'kwalify/util/hashlike'
module Babel
##
class Team
include Kwalify::Util::HashLike
attr_accessor :name # str
attr_accessor :desc # str
attr_accessor :chief # map
attr_accessor :members # seq
end
##
class Member
include Kwalify::Util::HashLike
attr_accessor :name # str
attr_accessor :desc # str
attr_accessor :team # map
end
end
$ kwalify -a genclass-ruby -P -f BABEL.schema.yaml \
--hashlike --initialize=false --module=Babel > models.rb
</pre>
<p>Here is the ruby program.
</p>
<a name="loadbabel.rb"></a>
<div class="program_caption">
loadbabel.rb</div>
<pre class="program">require 'kwalify'
<strong>require 'models'</strong>
## load schema definition
schema = Kwalify::Yaml.load_file('BABEL.schema.yaml',
:untabify=>true,
:preceding_alias=>true)
## add module name to 'class:'
Kwalify::Util.traverse_schema(schema) do |rulehash|
if rulehash['class']
rulehash['class'] = 'Babel::' + rulehash['class']
end
end
## create validator
validator = Kwalify::Validator.new(schema)
## parse with data-binding
parser = Kwalify::Yaml::Parser.new(validator)
parser.preceding_alias = true
<strong>parser.data_binding = true</strong>
ydoc = parser.parse_file('BABEL.data.yaml', :untabify=>true)
## show document
require 'pp'
pp ydoc
</pre>
<a name="howto5_databinding.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ ruby loadbabel.rb
{"teams"=>
[#<Babel::Team:0x53e0f8
@chief=
#<Babel::Member:0x53d5e0
@desc="Scientist",
@name="Kohichi Minamoto",
@team=#<Babel::Team:0x53e0f8 ...>>,
@desc="Level 7 ESPers",
@members=
[#<Babel::Member:0x53d018
@desc="Psychokino",
@name="Kaoru Akashi",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53ca50
@desc="Teleporter",
@name="Aoi Nogami",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53c488
@desc="Psycometrer",
@name="Shiho Sannomiya",
@team=#<Babel::Team:0x53e0f8 ...>>],
@name="The Children">],
"members"=>
[#<Babel::Member:0x53d5e0
@desc="Scientist",
@name="Kohichi Minamoto",
@team=
#<Babel::Team:0x53e0f8
@chief=#<Babel::Member:0x53d5e0 ...>,
@desc="Level 7 ESPers",
@members=
[#<Babel::Member:0x53d018
@desc="Psychokino",
@name="Kaoru Akashi",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53ca50
@desc="Teleporter",
@name="Aoi Nogami",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53c488
@desc="Psycometrer",
@name="Shiho Sannomiya",
@team=#<Babel::Team:0x53e0f8 ...>>],
@name="The Children">>,
#<Babel::Member:0x53d018
@desc="Psychokino",
@name="Kaoru Akashi",
@team=
#<Babel::Team:0x53e0f8
@chief=
#<Babel::Member:0x53d5e0
@desc="Scientist",
@name="Kohichi Minamoto",
@team=#<Babel::Team:0x53e0f8 ...>>,
@desc="Level 7 ESPers",
@members=
[#<Babel::Member:0x53d018 ...>,
#<Babel::Member:0x53ca50
@desc="Teleporter",
@name="Aoi Nogami",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53c488
@desc="Psycometrer",
@name="Shiho Sannomiya",
@team=#<Babel::Team:0x53e0f8 ...>>],
@name="The Children">>,
#<Babel::Member:0x53ca50
@desc="Teleporter",
@name="Aoi Nogami",
@team=
#<Babel::Team:0x53e0f8
@chief=
#<Babel::Member:0x53d5e0
@desc="Scientist",
@name="Kohichi Minamoto",
@team=#<Babel::Team:0x53e0f8 ...>>,
@desc="Level 7 ESPers",
@members=
[#<Babel::Member:0x53d018
@desc="Psychokino",
@name="Kaoru Akashi",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53ca50 ...>,
#<Babel::Member:0x53c488
@desc="Psycometrer",
@name="Shiho Sannomiya",
@team=#<Babel::Team:0x53e0f8 ...>>],
@name="The Children">>,
#<Babel::Member:0x53c488
@desc="Psycometrer",
@name="Shiho Sannomiya",
@team=
#<Babel::Team:0x53e0f8
@chief=
#<Babel::Member:0x53d5e0
@desc="Scientist",
@name="Kohichi Minamoto",
@team=#<Babel::Team:0x53e0f8 ...>>,
@desc="Level 7 ESPers",
@members=
[#<Babel::Member:0x53d018
@desc="Psychokino",
@name="Kaoru Akashi",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53ca50
@desc="Teleporter",
@name="Aoi Nogami",
@team=#<Babel::Team:0x53e0f8 ...>>,
#<Babel::Member:0x53c488 ...>],
@name="The Children">>]}
</pre>
<br>
<br>
<a name="actions"></a>
<h2 class="section1">Actions</h2>
<p>Kwalify has the command-line '-a <em>action</em>' which perform a certain action to schema definition.
Currently only the following actions are provided.
</p>
<dl class="dl2">
<dt class="dt2">
genclass-ruby</dt>
<dd class="dd2">
<p> Generate class definitions in Ruby.
</p>
</dd>
<dt class="dt2">
genclass-java</dt>
<dd class="dd2">
<p> Generate class definitions in Java.
</p>
</dd>
<dt class="dt2">
genclass-php</dt>
<dd class="dd2">
<p> Generate class definitions in Ruby.
</p>
</dd>
</dl>
<p>In fact action name represents template filename.
For example, action 'genclass-ruby' invokes template file 'kwalify/templates/genclass-ruby.eruby'.
</p>
<p>Each action can accept some command-line properties.
For example, action 'genclass-ruby' can accept the command-line properties '--module=<em>name</em>', '--parent=<em>name</em>', and so on.
Type 'kwalify -h -a <em>action</em>' to show the list of command-line properties the action can accept.
</p>
<p>It is able to add your on action template file.
The command-line option '-I' (template path) will help you.
</p>
<a name="action-genclass"></a>
<h3 class="section2">Class Definition Generation</h3>
<p>Command-line option '-a genclass-ruby' or '-a genclass-java' generates class definition
automatically from schema definition in Ruby or Java.
</p>
<p>Assume the following data file and schema definition.
</p>
<a name="address_book.yaml"></a>
<div class="program_caption">
<code>address_book.yaml</code> : data file</div>
<pre class="program">groups:
- name: family
desc: my family
- name: friend
desc: my friends
- name: business
desc: those who works together
people:
- name: Sumire
group: family
birth: 2000-01-01
blood: A
- name: Shiina
group: friend
birth: 1995-01-01
email: shiina@mail.org
- name: Sakura
group: business
email: cherry@mail.net
phone: 012-345-6789
</pre>
<a name="address_book.schema.yaml"></a>
<div class="program_caption">
<code>address_book.schema.yaml</code> : schema definition file</div>
<pre class="program">type: map
<strong>class: AddressBook</strong>
desc: address-book class
mapping:
"groups":
type: seq
sequence:
- type: map
<strong>class: Group</strong>
desc: group class
mapping:
"name": { type: str, required: yes }
"desc": { type: str }
"people":
type: seq
sequence:
- type: map
<strong>class: Person</strong>
desc: person class
mapping:
"name": { type: str, required: yes }
"desc": { type: str }
"group": { type: str }
"email": { type: str, pattern: '/@/' }
"phone": { type: str }
"birth": { type: date }
"blood": { type: str, enum: [A, B, O, AB] }
"deleted": { type: bool, <strong>default: false</strong> }
</pre>
<a name="action-genclass-ruby"></a>
<h4 class="section3">Ruby Class Definition</h4>
<div class="terminal_caption">
generate class definition</div>
<pre class="terminal">$ kwalify <strong>-a genclass-ruby</strong> -tf address_book.schema.yaml > address_book.rb
</pre>
<a name="address_book.rb"></a>
<div class="program_caption">
<code>address_book.rb</code> : generated class definition</div>
<pre class="program">## address-book class
class AddressBook
def initialize(hash=nil)
if hash.nil?
return
end
@groups = (v=hash['groups']) ? v.map!{|e| e.is_a?(Group) ? e : Group.new(e)} : v
@people = (v=hash['people']) ? v.map!{|e| e.is_a?(Person) ? e : Person.new(e)} : v
end
attr_accessor :groups # seq
attr_accessor :people # seq
end
## group class
class Group
def initialize(hash=nil)
if hash.nil?
return
end
@name = hash['name']
@desc = hash['desc']
end
attr_accessor :name # str
attr_accessor :desc # str
end
## person class
class Person
def initialize(hash=nil)
if hash.nil?
@deleted = false
return
end
@name = hash['name']
@desc = hash['desc']
@group = hash['group']
@email = hash['email']
@phone = hash['phone']
@birth = hash['birth']
@blood = hash['blood']
@deleted = (v=hash['deleted']).nil? ? false : v
end
attr_accessor :name # str
attr_accessor :desc # str
attr_accessor :group # str
attr_accessor :email # str
attr_accessor :phone # str
attr_accessor :birth # date
attr_accessor :blood # str
attr_accessor :deleted # bool
def deleted? ; @deleted ; end
end
</pre>
<a name="example_address_book.rb"></a>
<div class="program_caption">
<code>example_address_book.rb</code> : example code of using address-book.rb</div>
<pre class="program">require 'address_book'
require 'yaml'
require 'pp'
str = File.read('address_book.yaml')
ydoc = YAML.load(str)
<strong>addrbook = AddressBook.new(ydoc)</strong>
pp <strong>addrbook.groups</strong>
pp <strong>addrbook.people</strong>
</pre>
<a name="example_address_book_ruby.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ ruby example_address_book.rb
[#<Group:0xddf24 @desc="my family", @name="family">,
#<Group:0xddf10 @desc="my friends", @name="friend">,
#<Group:0xdde84 @desc="those who works together", @name="business">]
[#<Person:0xdefdc
@birth=#<Date: 4903089/2,0,2299161>,
@blood="A",
@deleted=false,
@desc=nil,
@email=nil,
@group="family",
@name="Sumire",
@phone=nil>,
#<Person:0xdee9c
@birth=#<Date: 4899437/2,0,2299161>,
@blood=nil,
@deleted=false,
@desc=nil,
@email="shiina@mail.org",
@group="friend",
@name="Shiina",
@phone=nil>,
#<Person:0xde8e8
@birth=nil,
@blood=nil,
@deleted=false,
@desc=nil,
@email="cherry@mail.net",
@group="business",
@name="Sakura",
@phone="012-345-6789">]
</pre>
<p>Command-line option '<code>-h -a genclass-ruby</code>' shows the commpand-line properties that template can accept.
</p>
<a name="option_ha.result"></a>
<div class="terminal_caption">
show command-line properties</div>
<pre class="terminal">$ kwalify -ha genclass-ruby
--module=name : module name in which class defined
--parent=name : parent class name
--include=name : module name which all classes include
--initialize=false : not print initialize() method
--hashlike : include Kwalify::Util::HashLike module
</pre>
<div class="terminal_caption">
example of command-line properties</div>
<pre class="terminal">$ kwalify -a genclass-ruby --module=My --hashlike
</pre>
<p>If command-line property '--hashlike' (== '--hashlike=true') is specified,
module Kwalify::Util::HashLike is included for each classes generated.
That module is defined in 'kwalify/util/hashlike.rb'
</p>
<br>
<a name="action-genclass-java"></a>
<h4 class="section3">Java Class Definition</h4>
<a name="genclass_java.result"></a>
<div class="terminal_caption">
generate java class definition</div>
<pre class="terminal">$ kwalify <strong>-a genclass-java</strong> -tf address_book.schema.yaml
generating ./AddressBook.java...done.
generating ./Group.java...done.
generating ./Person.java...done.
</pre>
<a name="AddressBook.java.expected"></a>
<div class="program_caption">
<code>AddressBook.java</code> : generated class definition</div>
<pre class="program">// generated by kwalify from address_book.schema.yaml
import java.util.*;
/**
* address-book class
*/
public class AddressBook {
private List _groups;
private List _people;
public AddressBook() {}
public AddressBook(Map map) {
List seq;
Object obj;
if ((seq = (List)map.get("groups")) != null) {
for (int i = 0; i < seq.size(); i++) {
if ((obj = seq.get(i)) instanceof Map) {
seq.set(i, new Group((Map)obj));
}
}
}
_groups = seq;
if ((seq = (List)map.get("people")) != null) {
for (int i = 0; i < seq.size(); i++) {
if ((obj = seq.get(i)) instanceof Map) {
seq.set(i, new Person((Map)obj));
}
}
}
_people = seq;
}
public List getGroups() { return _groups; }
public void setGroups(List groups_) { _groups = groups_; }
public List getPeople() { return _people; }
public void setPeople(List people_) { _people = people_; }
}
</pre>
<a name="Group.java.expected"></a>
<div class="program_caption">
<code>Group.java</code> : generated class definition</div>
<pre class="program">// generated by kwalify from address_book.schema.yaml
import java.util.*;
/**
* group class
*/
public class Group {
private String _name;
private String _desc;
public Group() {}
public Group(Map map) {
_name = (String)map.get("name");
_desc = (String)map.get("desc");
}
public String getName() { return _name; }
public void setName(String name_) { _name = name_; }
public String getDesc() { return _desc; }
public void setDesc(String desc_) { _desc = desc_; }
}
</pre>
<a name="Person.java.expected"></a>
<div class="program_caption">
<code>Person.java</code> : generated class definition</div>
<pre class="program">// generated by kwalify from address_book.schema.yaml
import java.util.*;
/**
* person class
*/
public class Person {
private String _name;
private String _desc;
private String _group;
private String _email;
private String _phone;
private Date _birth;
private String _blood;
public Person() {}
public Person(Map map) {
_name = (String)map.get("name");
_desc = (String)map.get("desc");
_group = (String)map.get("group");
_email = (String)map.get("email");
_phone = (String)map.get("phone");
_birth = (Date)map.get("birth");
_blood = (String)map.get("blood");
}
public String getName() { return _name; }
public void setName(String name_) { _name = name_; }
public String getDesc() { return _desc; }
public void setDesc(String desc_) { _desc = desc_; }
public String getGroup() { return _group; }
public void setGroup(String group_) { _group = group_; }
public String getEmail() { return _email; }
public void setEmail(String email_) { _email = email_; }
public String getPhone() { return _phone; }
public void setPhone(String phone_) { _phone = phone_; }
public Date getBirth() { return _birth; }
public void setBirth(Date birth_) { _birth = birth_; }
public String getBlood() { return _blood; }
public void setBlood(String blood_) { _blood = blood_; }
}
</pre>
<a name="ExampleAddressBook.java"></a>
<div class="program_caption">
<code>ExampleAddressBook.java</code> : example code of using *.java</div>
<pre class="program">import java.util.*;
import kwalify.*;
public class ExampleAddressBook {
public static void main(String args[]) throws Exception {
// read schema
String schema_str = Util.readFile("address_book.schema.yaml");
schema_str = Util.untabify(schema_str);
Object schema = new YamlParser(schema_str).parse();
// read document file
String document_str = Util.readFile("address_book.yaml");
document_str = Util.untabify(document_str);
YamlParser parser = new YamlParser(document_str);
Object document = parser.parse();
// create address book object
AddressBook addrbook = new AddressBook((Map)document);
// show groups
List groups = addrbook.getGroups();
if (groups != null) {
for (Iterator it = groups.iterator(); it.hasNext(); ) {
Group group = (Group)it.next();
System.out.println("group name: " + group.getName());
System.out.println("group desc: " + group.getDesc());
System.out.println();
}
}
// show people
List people = addrbook.getPeople();
if (people != null) {
for (Iterator it = people.iterator(); it.hasNext(); ) {
Person person = (Person)it.next();
System.out.println("person name: " + person.getName());
System.out.println("person group: " + person.getGroup());
System.out.println("person email: " + person.getEmail());
System.out.println("person phone: " + person.getPhone());
System.out.println("person blood: " + person.getBlood());
System.out.println("person birth: " + person.getBirth());
System.out.println();
}
}
}
}
</pre>
<a name="example_address_book_java.result"></a>
<div class="terminal_caption">
result</div>
<pre class="terminal">$ javac -classpath '.:kwalify.jar' *.java
$ java -classpath '.:kwalify.jar' ExampleAddressBook
group name: family
group desc: my family
group name: friend
group desc: my friends
group name: business
group desc: those who works together
person name: Sumire
person group: family
person email: null
person phone: null
person blood: A
person birth: Tue Feb 01 00:00:00 JST 2000
person name: Shiina
person group: friend
person email: shiina@mail.org
person phone: null
person blood: null
person birth: Wed Feb 01 00:00:00 JST 1995
person name: Sakura
person group: business
person email: cherry@mail.net
person phone: 012-345-6789
person blood: null
person birth: null
</pre>
<p>Command-line option '<code>-h -a genclass-java</code>' shows the commpand-line properties that template can accept.
</p>
<a name="option_ha_genclass_java.result"></a>
<div class="terminal_caption">
show command-line properties</div>
<pre class="terminal">$ kwalify -ha genclass-java
--package=name : package name
--extends=name : class name to extend
--implements=name,... : interface names to implement
--dir=path : directory to locate output file
--basedir=path : base directory to locate output file
--constructor=false : not print initialize() method
</pre>
<div class="terminal_caption">
example of command-line properties</div>
<pre class="terminal">$ kwalify -a genclass-java --package=com.example.my --implements=Serializable --basedir=src
</pre>
<br>
<br>
<br>
<a name="ref"></a>
<h2 class="section1">References</h2>
<a name="ref-usage"></a>
<h3 class="section2">Usage in Command-Line</h3>
<div class="terminal_caption">
</div>
<pre class="terminal">### usage1: validate YAML document in command-line
$ kwalify -f schema.yaml document.yaml [document2.yaml ...]
### usage2: validate schema definition in command-line
$ kwalify -m schema.yaml [schema2.yaml ...]
</pre>
<p>Command-line options:
</p>
<dl class="dl3">
<dt class="dt3"><strong>
<code>-h</code>, <code>--help</code> </strong></dt>
<dd class="dd3">
Print help message.
</dd>
<dt class="dt3"><strong>
<code>-v</code> </strong></dt>
<dd class="dd3">
Print version.
</dd>
<dt class="dt3"><strong>
<code>-q</code> </strong></dt>
<dd class="dd3">
Quiet mode.
</dd>
<dt class="dt3"><strong>
<code>-s</code> </strong></dt>
<dd class="dd3">
(Obsolete. Use '-q' instead.) Silent mode.
</dd>
<dt class="dt3"><strong>
<code>-f <em>schema.yaml</em></code> </strong></dt>
<dd class="dd3">
Specify schema definition file.
</dd>
<dt class="dt3"><strong>
<code>-m</code> </strong></dt>
<dd class="dd3">
Meta-validation of schema definition.
</dd>
<dt class="dt3"><strong>
<code>-t</code> </strong></dt>
<dd class="dd3">
Expand tab characters to spaces automatically.
</dd>
<dt class="dt3"><strong>
<code>-l</code> </strong></dt>
<dd class="dd3">
Show linenumber on which error found.
</dd>
<dt class="dt3"><strong>
<code>-E</code> </strong></dt>
<dd class="dd3">
Show errors in Emacs-compatible style (implies '-l' option).
</dd>
<dt class="dt3"><strong>
<code>-a action</code> </strong></dt>
<dd class="dd3">
Do action. Currently supported action is 'genclass-ruby' and 'genclass-java'.
Try '-ha action' to get help about the action.
</dd>
<dt class="dt3"><strong>
<code>-I path1,path2,...</code> </strong></dt>
<dd class="dd3">
Template path (for '-a').
</dd>
<dt class="dt3"><strong>
<code>-P</code> </strong></dt>
<dd class="dd3">
Enable preceding alias.
</dd>
</dl>
<br>
<br>
</div>
</body>
</html>
|