kwalify 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,8 +1,13 @@
1
1
  .=title: ChangeLog
2
- .?release: $Release: 0.3.0 $
2
+ .?release: $Release: 0.4.0 $
3
3
  .?lastupdate: $Date$
4
4
  .?version: $Rev$
5
5
 
6
+ .: 2005-10-25 (release 0.4.0)
7
+ .* Enhances:
8
+ .- New command-line option '-l' prints error line numbers.
9
+ .- Supports default rule of mapping.
10
+
6
11
  .: 2005-09-30 (release 0.3.0)
7
12
  .* Enhances:
8
13
  .- Support 'max-ex' and 'min-ex' (max/min exclusive) support with 'range:'
data/README.txt CHANGED
@@ -1,7 +1,7 @@
1
1
  .=title: README
2
2
  .?version: $Rev: 18 $
3
3
  .?lastupdate: $Date: 2005-09-25 22:36:35 +0900 (Sun, 25 Sep 2005) $
4
- .?release: $Release: 0.3.0 $
4
+ .?release: $Release: 0.4.0 $
5
5
 
6
6
 
7
7
  .$ About Kwalify
data/bin/kwalify CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ###
4
4
  ### $Rev: 12 $
5
- ### $Release: 0.3.0 $
5
+ ### $Release: 0.4.0 $
6
6
  ### copyright(c) 2005 kuwata-lab all rights reserved.
7
7
  ###
8
8
 
data/doc/users-guide.html CHANGED
@@ -15,12 +15,12 @@
15
15
  <div align="left"><h1>Kwalify Users' Guide</h1></div>
16
16
  <div align="left">
17
17
  makoto kuwata &lt;kwa(at)kuwata-lab.com&gt;<br>
18
- last update: $Date: 2005-10-02 22:45:14 +0900 (Sun, 02 Oct 2005) $<br>
18
+ last update: $Date: 2005-10-25 08:01:14 +0900 (Tue, 25 Oct 2005) $<br>
19
19
  </div>
20
20
 
21
21
  <a name="preface"></a>
22
22
  <h2 class="section1">Preface</h2>
23
- <p>Kwalify<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> is a tiny schema validator for YAML document.
23
+ <p>Kwalify<sup>(<a href="#fnref:1" name="fnlink:1">*1</a>)</sup> is a tiny schema validator for YAML and JSON document.
24
24
  </p>
25
25
  <p>Do you know "80-20 rule" known as Pareto Law? This rule suggests that 20% of the population owns 80% of the wealth.
26
26
  Kwalify is based on a new "50-5 rule" which suggests that 5% of the population owns 50 of the wealth.
@@ -36,9 +36,9 @@ This rule is more aggressive and cost-effective than Pareto Law. The rule is nam
36
36
  </tr>
37
37
  <tr class="tr1">
38
38
  <td class="td1">XML Schema</td>
39
- <td class="td1">96%</td>
40
- <td class="td1">80%</td>
41
- <td class="td1">1.2 (= 96/80)</td>
39
+ <td class="td1">95%</td>
40
+ <td class="td1">100%</td>
41
+ <td class="td1">0.95 (= 95/100)</td>
42
42
  </tr>
43
43
  <tr class="tr1">
44
44
  <td class="td1">RelaxNG</td>
@@ -80,7 +80,7 @@ Table of Contents:
80
80
  </li>
81
81
  <li><a href="#schema-map-of-seq">Mapping of Sequence</a>
82
82
  </li>
83
- <li><a href="#schema-rules">Rules</a>
83
+ <li><a href="#schema-rules">Rule and Constraint</a>
84
84
  </li>
85
85
  <li><a href="#schema-unique">Unique constraint</a>
86
86
  </li>
@@ -94,8 +94,14 @@ Table of Contents:
94
94
  <ul>
95
95
  <li><a href="#tips-quotes">Enclose Key Names in (Double) Quotes</a>
96
96
  </li>
97
+ <li><a href="#tips-json">JSON</a>
98
+ </li>
97
99
  <li><a href="#tips-anchor">Anchor</a>
98
100
  </li>
101
+ <li><a href="#tips-default">Default of Mapping</a>
102
+ </li>
103
+ <li><a href="#tips-merge">Merging Mappings</a>
104
+ </li>
99
105
  </ul>
100
106
  </li>
101
107
  </ul>
@@ -154,7 +160,14 @@ usage2: validate schema definition in command-line</div>
154
160
  <dd class="dd3">
155
161
  Expand tab characters to spaces automatically.
156
162
  </dd>
163
+ <dt class="dt3"><strong>
164
+ <code>-l</code> </strong></dt>
165
+ <dd class="dd3">
166
+ Show linenumber on which error found.
167
+ </dd>
157
168
  </dl>
169
+ <p>Notice that the command-line option <code>-l</code> is an experimental feature, for kwalify command use original YAML parser instead of Syck parser when this option is specified.
170
+ </p>
158
171
  <br>
159
172
 
160
173
 
@@ -177,6 +190,30 @@ unless error_list.empty?
177
190
  end
178
191
  end
179
192
  </pre>
193
+ <div class="program_caption">
194
+ validate YAML document and show linenumber on where error is found.</div>
195
+ <pre class="program">require 'kwalify'
196
+
197
+ ## parse schema definition and create validator
198
+ schema = YAML.load_file('schema.yaml')
199
+ validator = Kwalify::Validator.new(schema) # raises Kwalify::SchemaError if wrong
200
+
201
+ ## parse YAML document with Kwalify's parser
202
+ str = File.read('document.yaml')
203
+ parser = Kwalify::Parser.new(str)
204
+ document = parser.parse()
205
+
206
+ ## validate document and show errors
207
+ error_list = validator.validate(document)
208
+ unless error_list.empty?
209
+ parser.set_error_linenums(error_list) # set linenum on error
210
+ error_list.sort { |e1, e2| e1.linenum &lt;=&gt; e2.linenum }.each do |error|
211
+ puts "(line %d)[%s] %s" % [error.linenum, error.path, error.message]
212
+ end
213
+ end
214
+ </pre>
215
+ <p>Kwalify's YAML parser is experimental. You should notice that Kwalify's YAML parser is limited only for basic syntax of YAML.
216
+ </p>
180
217
  <br>
181
218
 
182
219
 
@@ -203,7 +240,7 @@ sequence:
203
240
  </pre>
204
241
  <div class="terminal_caption">
205
242
  validate</div>
206
- <pre class="terminal">$ kwalify -f schema01.yaml document01a.yaml
243
+ <pre class="terminal">$ kwalify -lf schema01.yaml document01a.yaml
207
244
  document01a.yaml#0: valid.
208
245
  </pre>
209
246
  <a name="document01b.yaml"></a>
@@ -215,9 +252,9 @@ document01a.yaml#0: valid.
215
252
  </pre>
216
253
  <div class="terminal_caption">
217
254
  validate</div>
218
- <pre class="terminal">$ kwalify -f schema01.yaml document01b.yaml
255
+ <pre class="terminal">$ kwalify -lf schema01.yaml document01b.yaml
219
256
  document01b.yaml#0: INVALID
220
- - [/1] '123': not a string.
257
+ - (line 2) [/1] '123': not a string.
221
258
  </pre>
222
259
  <p>Default '<code>type:</code>' is <code>str</code> so you can omit '<code>type: str</code>'.
223
260
  </p>
@@ -252,7 +289,7 @@ birth: 1985-01-01
252
289
  </pre>
253
290
  <div class="terminal_caption">
254
291
  validate</div>
255
- <pre class="terminal">$ kwalify -f schema02.yaml document02a.yaml
292
+ <pre class="terminal">$ kwalify -lf schema02.yaml document02a.yaml
256
293
  document02a.yaml#0: valid.
257
294
  </pre>
258
295
  <a name="document02b.yaml"></a>
@@ -265,11 +302,11 @@ birth: Jun 01, 1985
265
302
  </pre>
266
303
  <div class="terminal_caption">
267
304
  validate</div>
268
- <pre class="terminal">$ kwalify -f schema02.yaml document02b.yaml
305
+ <pre class="terminal">$ kwalify -lf schema02.yaml document02b.yaml
269
306
  document02b.yaml#0: INVALID
270
- - [/birth] 'Jun 01, 1985': not a date.
271
- - [/age] 'twenty': not a integer.
272
- - [/email] 'foo(at)mail.com': not matched to pattern /@/.
307
+ - (line 2) [/email] 'foo(at)mail.com': not matched to pattern /@/.
308
+ - (line 3) [/age] 'twenty': not a integer.
309
+ - (line 4) [/birth] 'Jun 01, 1985': not a date.
273
310
  </pre>
274
311
  <br>
275
312
 
@@ -301,7 +338,7 @@ sequence:
301
338
  </pre>
302
339
  <div class="terminal_caption">
303
340
  validate</div>
304
- <pre class="terminal">$ kwalify -f schema03.yaml document03a.yaml
341
+ <pre class="terminal">$ kwalify -lf schema03.yaml document03a.yaml
305
342
  document03a.yaml#0: valid.
306
343
  </pre>
307
344
  <a name="document03b.yaml"></a>
@@ -316,11 +353,11 @@ document03a.yaml#0: valid.
316
353
  </pre>
317
354
  <div class="terminal_caption">
318
355
  validate</div>
319
- <pre class="terminal">$ kwalify -f schema03.yaml document03b.yaml
356
+ <pre class="terminal">$ kwalify -lf schema03.yaml document03b.yaml
320
357
  document03b.yaml#0: INVALID
321
- - [/1] key 'name:' is required.
322
- - [/1] key 'naem:' is undefined.
323
- - [/2] key 'mail:' is undefined.
358
+ - (line 3) [/1] key 'name:' is required.
359
+ - (line 3) [/1] key 'naem:' is undefined.
360
+ - (line 5) [/2] key 'mail:' is undefined.
324
361
  </pre>
325
362
  <br>
326
363
 
@@ -366,7 +403,7 @@ employees:
366
403
  </pre>
367
404
  <div class="terminal_caption">
368
405
  validate</div>
369
- <pre class="terminal">$ kwalify -f schema04.yaml document04a.yaml
406
+ <pre class="terminal">$ kwalify -lf schema04.yaml document04a.yaml
370
407
  document04a.yaml#0: valid.
371
408
  </pre>
372
409
  <a name="document04b.yaml"></a>
@@ -384,16 +421,18 @@ employees:
384
421
  </pre>
385
422
  <div class="terminal_caption">
386
423
  validate</div>
387
- <pre class="terminal">$ kwalify -f schema04.yaml document04b.yaml
424
+ <pre class="terminal">$ kwalify -lf schema04.yaml document04b.yaml
388
425
  document04b.yaml#0: INVALID
389
- - [/employees/0/code] 'A101': not a integer.
390
- - [/employees/1] key 'mail:' is undefined.
426
+ - (line 4) [/employees/0/code] 'A101': not a integer.
427
+ - (line 7) [/employees/1] key 'mail:' is undefined.
391
428
  </pre>
392
429
  <br>
393
430
 
394
431
 
395
432
  <a name="schema-rules"></a>
396
- <h3 class="section2">Rules</h3>
433
+ <h3 class="section2">Rule and Constraint</h3>
434
+ <p>The followings are constraints.
435
+ </p>
397
436
  <dl class="dl3">
398
437
  <dt class="dt3"><strong>
399
438
  <code>required:</code> </strong></dt>
@@ -456,12 +495,23 @@ document04b.yaml#0: INVALID
456
495
  <dt class="dt3"><strong>
457
496
  <code>range:</code> </strong></dt>
458
497
  <dd class="dd3">
459
- Range of value between max and min. Type <code>seq</code>, <code>map</code>, <code>bool</code> and <code>any</code> are not available with <code>range:</code>.
498
+ Range of value between max/max-ex and min/min-ex.
499
+ <ul type="circle">
500
+ <li>'max' means 'max-inclusive'.
501
+ </li>
502
+ <li>'min' means 'min-inclusive'.
503
+ </li>
504
+ <li>'max-ex' means 'max-exclusive'.
505
+ </li>
506
+ <li>'min-ex' means 'min-exclusive'.
507
+ </li>
508
+ </ul>
509
+ Type <code>seq</code>, <code>map</code>, <code>bool</code> and <code>any</code> are not available with <code>range:</code>.
460
510
  </dd>
461
511
  <dt class="dt3"><strong>
462
512
  <code>length:</code> </strong></dt>
463
513
  <dd class="dd3">
464
- Range of length of value between max and min. Only type <code>str</code> and <code>text</code> are available with <code>length:</code>.
514
+ 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>.
465
515
  </dd>
466
516
  <dt class="dt3"><strong>
467
517
  <code>assert:</code> </strong></dt>
@@ -475,6 +525,8 @@ document04b.yaml#0: INVALID
475
525
  Value is unique for mapping or sequence. See the next subsection for detail.
476
526
  </dd>
477
527
  </dl>
528
+ <p>A group of constraints is called "rule". For example, constraints which are element value of 'sequence:' or 'mapping:' are rules.
529
+ </p>
478
530
  <a name="schema05.yaml"></a>
479
531
  <div class="program_caption">
480
532
  <code>schema05.yaml</code> : rules examples</div>
@@ -495,7 +547,7 @@ sequence:
495
547
  age:
496
548
  type: int
497
549
  range: { max: 30, min: 18 }
498
- # or assert: 30 &lt;= val &amp;&amp; val &lt;= 18
550
+ # or assert: 18 &lt;= val &amp;&amp; val &lt;= 30
499
551
  blood:
500
552
  type: str
501
553
  enum:
@@ -525,7 +577,7 @@ sequence:
525
577
  </pre>
526
578
  <div class="terminal_caption">
527
579
  validate</div>
528
- <pre class="terminal">$ kwalify -f schema05.yaml document05a.yaml
580
+ <pre class="terminal">$ kwalify -lf schema05.yaml document05a.yaml
529
581
  document05a.yaml#0: valid.
530
582
  </pre>
531
583
  <a name="document05b.yaml"></a>
@@ -546,17 +598,17 @@ document05a.yaml#0: valid.
546
598
  </pre>
547
599
  <div class="terminal_caption">
548
600
  validate</div>
549
- <pre class="terminal">$ kwalify -f schema05.yaml document05b.yaml
601
+ <pre class="terminal">$ kwalify -lf schema05.yaml document05b.yaml
550
602
  document05b.yaml#0: INVALID
551
- - [/0/blood] 'a': invalid blood value.
552
- - [/0/age] 'twenty': not a integer.
553
- - [/0/password] 'xxx123': too short (length 6 &lt; min 8).
554
- - [/0/email] 'foo(at)mail.com': not matched to pattern /@/.
555
- - [/1] key 'name:' is required.
556
- - [/1] key 'family-name:' is undefined.
557
- - [/1] key 'given-name:' is undefined.
558
- - [/1/birth] '1980/01/01': not a date.
559
- - [/1/age] '15': too small (&lt; min 18).
603
+ - (line 2) [/0/email] 'foo(at)mail.com': not matched to pattern /@/.
604
+ - (line 3) [/0/password] 'xxx123': too short (length 6 &lt; min 8).
605
+ - (line 4) [/0/age] 'twenty': not a integer.
606
+ - (line 5) [/0/blood] 'a': invalid blood value.
607
+ - (line 7) [/1] key 'given-name:' is undefined.
608
+ - (line 7) [/1] key 'family-name:' is undefined.
609
+ - (line 7) [/1] key 'name:' is required.
610
+ - (line 10) [/1/age] '15': too small (&lt; min 18).
611
+ - (line 12) [/1/birth] '1980/01/01': not a date.
560
612
  </pre>
561
613
  <br>
562
614
 
@@ -566,12 +618,12 @@ document05b.yaml#0: INVALID
566
618
  <p>'<code>unique:</code>' constraint is available with elements of sequence or mapping.
567
619
  This is equivalent to unique key or primary key of RDBMS.
568
620
  </p>
569
- <p>Type of a rule which has '<code>unique:</code>' constraint should be scalar (str, int, float, ...).
570
- Type of parent rule should be sequence or mapping.
621
+ <p>Type of a rule which has '<code>unique:</code>' constraint must be scalar (str, int, float, ...).
622
+ Type of parent rule must be sequence or mapping.
571
623
  </p>
572
624
  <a name="schema06.yaml"></a>
573
625
  <div class="program_caption">
574
- <code>schema06.yaml</code> : unique constraint with mapping</div>
626
+ <code>schema06.yaml</code> : unique constraint with mapping and sequence</div>
575
627
  <pre class="program">type: seq
576
628
  sequence:
577
629
  - type: map
@@ -610,7 +662,7 @@ sequence:
610
662
  </pre>
611
663
  <div class="terminal_caption">
612
664
  validate</div>
613
- <pre class="terminal">$ kwalify -f schema06.yaml document06a.yaml
665
+ <pre class="terminal">$ kwalify -lf schema06.yaml document06a.yaml
614
666
  document06a.yaml#0: valid.
615
667
  </pre>
616
668
  <a name="document06b.yaml"></a>
@@ -635,10 +687,10 @@ document06a.yaml#0: valid.
635
687
  </pre>
636
688
  <div class="terminal_caption">
637
689
  validate</div>
638
- <pre class="terminal">$ kwalify -f schema06.yaml document06b.yaml
690
+ <pre class="terminal">$ kwalify -lf schema06.yaml document06b.yaml
639
691
  document06b.yaml#0: INVALID
640
- - [/0/groups/3] 'foo': is already used at '/0/groups/0'.
641
- - [/2/name] 'bar': is already used at '/1/name'.
692
+ - (line 7) [/0/groups/3] 'foo': is already used at '/0/groups/0'.
693
+ - (line 13) [/2/name] 'bar': is already used at '/1/name'.
642
694
  </pre>
643
695
  <br>
644
696
 
@@ -653,11 +705,11 @@ This method is called by Kwalify::Validator#validate().
653
705
  schema07.yaml : 'name:' is important.</div>
654
706
  <pre class="program">type: map
655
707
  mapping:
656
- questionnaires:
708
+ answers:
657
709
  type: seq
658
710
  sequence:
659
711
  - type: map
660
- <strong>name: Questionnaire</strong>
712
+ <strong>name: Answer</strong>
661
713
  mapping:
662
714
  name:
663
715
  type: str
@@ -680,8 +732,8 @@ validate07.rb : validate script</div>
680
732
  require 'kwalify'
681
733
  require 'yaml'
682
734
 
683
- ## validator class for questionnaires
684
- class QuestionnairesValidator &lt; Kwalify::Validator
735
+ ## validator class for answers
736
+ class AnswersValidator &lt; Kwalify::Validator
685
737
 
686
738
  ## load schema definition
687
739
  @@schema = YAML.load_file('schema07.yaml')
@@ -693,7 +745,7 @@ class QuestionnairesValidator &lt; Kwalify::Validator
693
745
  ## hook method called by Validator#validate()
694
746
  <strong>def validate_hook(value, rule, path, errors)</strong>
695
747
  <strong>case rule.name</strong>
696
- <strong>when 'Questionnaire'</strong>
748
+ <strong>when 'Answer'</strong>
697
749
  if value['answer'] == 'bad'
698
750
  reason = value['reason']
699
751
  if !reason || reason.empty?
@@ -707,7 +759,7 @@ class QuestionnairesValidator &lt; Kwalify::Validator
707
759
  end
708
760
 
709
761
  ## create validator
710
- validator = QuestionnairesValidator.new
762
+ validator = AnswersValidator.new
711
763
 
712
764
  ## load YAML document
713
765
  input = ARGF.read()
@@ -728,7 +780,7 @@ end
728
780
  <a name="document07a.yaml"></a>
729
781
  <div class="program_caption">
730
782
  <code>document07a.yaml</code> : valid document example</div>
731
- <pre class="program">questionnaires:
783
+ <pre class="program">answers:
732
784
  - name: Foo
733
785
  answer: good
734
786
  reason: I like this style.
@@ -746,7 +798,7 @@ Valid.
746
798
  <a name="document07b.yaml"></a>
747
799
  <div class="program_caption">
748
800
  <code>document07b.yaml</code> : invalid document example</div>
749
- <pre class="program">questionnaires:
801
+ <pre class="program">answers:
750
802
  - name: Foo
751
803
  answer: good
752
804
  - name: Bar
@@ -758,7 +810,7 @@ Valid.
758
810
  validate</div>
759
811
  <pre class="terminal">$ ruby validate07.rb document07b.yaml
760
812
  *** INVALID!
761
- - [/questionnaires/1] : reason is required when answer is 'bad'.
813
+ - [/answers/1] : reason is required when answer is 'bad'.
762
814
  </pre>
763
815
  <br>
764
816
 
@@ -780,10 +832,10 @@ require 'yaml'
780
832
  ## load schema definition
781
833
  schema = YAML.load_file('schema06.yaml')
782
834
 
783
- ## create validator for questionnaires
835
+ ## create validator for answers
784
836
  validator = Kwalify::Validator.new(schema) <strong>{ |value, rule, path, errors|</strong>
785
837
  <strong>case rule.name</strong>
786
- <strong>when 'Questionnaire'</strong>
838
+ <strong>when 'Answer'</strong>
787
839
  if value['answer'] == 'bad'
788
840
  reason = value['reason']
789
841
  if !reason || reason.empty?
@@ -866,13 +918,90 @@ mapping:
866
918
  <br>
867
919
 
868
920
 
921
+ <a name="tips-json"></a>
922
+ <h3 class="section2">JSON</h3>
923
+ <p><a href="http://www.json.org">JSON</a> is a lightweight data-interchange format, especially useful for JavaScript.
924
+ JSON can be considered as a subset of YAML. It means that YAML parser can parse JSON and Kwalify can validate JSON document.
925
+ </p>
926
+ <a name="schema12.yaml"></a>
927
+ <div class="program_caption">
928
+ <code>schema12.yaml</code> : an example schema written in JSON format</div>
929
+ <pre class="program">{ "type": "map",
930
+ "required": true,
931
+ "mapping": {
932
+ "name": {
933
+ "type": "str",
934
+ "required": true
935
+ },
936
+ "email": {
937
+ "type": "str"
938
+ },
939
+ "age": {
940
+ "type": "int"
941
+ },
942
+ "gender": {
943
+ "type": "str",
944
+ "enum": ["M", "F"]
945
+ },
946
+ "favorite": {
947
+ "type": "seq",
948
+ "sequence": [
949
+ { "type": "str" }
950
+ ]
951
+ }
952
+ }
953
+ }
954
+ </pre>
955
+ <a name="document12a.yaml"></a>
956
+ <div class="program_caption">
957
+ <code>document12a.yaml</code> : valid JSON document example</div>
958
+ <pre class="program">{ "name": "Foo",
959
+ "email": "foo@mail.com",
960
+ "age": 20,
961
+ "gender": "F",
962
+ "favorite": [
963
+ "football",
964
+ "basketball",
965
+ "baseball"
966
+ ]
967
+ }
968
+ </pre>
969
+ <div class="terminal_caption">
970
+ validate</div>
971
+ <pre class="terminal">$ kwalify -lf schema12.yaml document12a.yaml
972
+ document12a.yaml#0: valid.
973
+ </pre>
974
+ <a name="document12b.yaml"></a>
975
+ <div class="program_caption">
976
+ <code>document12b.yaml</code> : invalid JSON document example</div>
977
+ <pre class="program">{
978
+ "mail": "foo@mail.com",
979
+ "age": twenty,
980
+ "gender": "X",
981
+ "favorite": [ 123, 456 ]
982
+ }
983
+ </pre>
984
+ <div class="terminal_caption">
985
+ validate</div>
986
+ <pre class="terminal">$ kwalify -lf schema12.yaml document12b.yaml
987
+ document12b.yaml#0: INVALID
988
+ - (line 1) [/] key 'name:' is required.
989
+ - (line 1) [/] key 'mail:' is undefined.
990
+ - (line 3) [/age] 'twenty': not a integer.
991
+ - (line 4) [/gender] 'X': invalid gender value.
992
+ - (line 5) [/favorite/0] '123': not a string.
993
+ - (line 5) [/favorite/1] '456': not a string.
994
+ </pre>
995
+ <br>
996
+
997
+
869
998
  <a name="tips-anchor"></a>
870
999
  <h3 class="section2">Anchor</h3>
871
1000
  <p>You can share schemas with YAML anchor.
872
1001
  </p>
873
- <a name="schema12.yaml"></a>
1002
+ <a name="schema13.yaml"></a>
874
1003
  <div class="program_caption">
875
- <code>schema12.yaml</code> : anchor example</div>
1004
+ <code>schema13.yaml</code> : anchor example</div>
876
1005
  <pre class="program">type: seq
877
1006
  sequence:
878
1007
  - <strong>&amp;employee</strong>
@@ -891,9 +1020,9 @@ sequence:
891
1020
  </pre>
892
1021
  <p>Anchor is also available in YAML document.
893
1022
  </p>
894
- <a name="document12a.yaml"></a>
1023
+ <a name="document13a.yaml"></a>
895
1024
  <div class="program_caption">
896
- <code>document12a.yaml</code> : valid document example</div>
1025
+ <code>document13a.yaml</code> : valid document example</div>
897
1026
  <pre class="program">- <strong>&amp;foo</strong>
898
1027
  given-name: foo
899
1028
  family-name: Foo
@@ -914,8 +1043,145 @@ sequence:
914
1043
  </pre>
915
1044
  <div class="terminal_caption">
916
1045
  validate</div>
917
- <pre class="terminal">$ kwalify -f schema12.yaml document12a.yaml
918
- document12a.yaml#0: valid.
1046
+ <pre class="terminal">$ kwalify -lf schema13.yaml document13a.yaml
1047
+ document13a.yaml#0: valid.
1048
+ </pre>
1049
+ <br>
1050
+
1051
+
1052
+ <a name="tips-default"></a>
1053
+ <h3 class="section2">Default of Mapping</h3>
1054
+ <p>YAML allows user to specify default value of mapping.
1055
+ </p>
1056
+ <p>For example, the following YAML document uses default value of mapping.
1057
+ </p>
1058
+ <pre class="program">A: 10
1059
+ B: 20
1060
+ <strong>=: -1</strong> # default value
1061
+ </pre>
1062
+ <p>This is equal to the following Ruby code.
1063
+ </p>
1064
+ <pre class="program">map = ["A"=&gt;10, "B"=&gt;20]
1065
+ map.default = -1
1066
+ map
1067
+ </pre>
1068
+ <p>Kwalify allows user to specify default rule using default value of mapping.
1069
+ It is useful when key names are unknown.
1070
+ </p>
1071
+ <a name="schema14.yaml"></a>
1072
+ <div class="program_caption">
1073
+ <code>schema14.yaml</code> : default rule example</div>
1074
+ <pre class="program">type: map
1075
+ mapping:
1076
+ <strong>=:</strong> # default rule
1077
+ type: number
1078
+ range: { max: 1, min: -1 }
1079
+ </pre>
1080
+ <a name="document14a.yaml"></a>
1081
+ <div class="program_caption">
1082
+ <code>document14a.yaml</code> : valid document example</div>
1083
+ <pre class="program">value1: 0
1084
+ value2: 0.5
1085
+ value3: -0.9
1086
+ </pre>
1087
+ <div class="terminal_caption">
1088
+ validate</div>
1089
+ <pre class="terminal">$ kwalify -lf schema14.yaml document14a.yaml
1090
+ document14a.yaml#0: valid.
1091
+ </pre>
1092
+ <a name="document14b.yaml"></a>
1093
+ <div class="program_caption">
1094
+ <code>document14b.yaml</code> : invalid document example</div>
1095
+ <pre class="program">value1: 0
1096
+ value2: 1.1
1097
+ value3: -2.0
1098
+ </pre>
1099
+ <div class="terminal_caption">
1100
+ validate</div>
1101
+ <pre class="terminal">$ kwalify -lf schema14.yaml document14b.yaml
1102
+ document14b.yaml#0: INVALID
1103
+ - (line 2) [/value2] '1.1': too large (&gt; max 1).
1104
+ - (line 3) [/value3] '-2.0': too small (&lt; min -1).
1105
+ </pre>
1106
+ <br>
1107
+
1108
+
1109
+ <a name="tips-merge"></a>
1110
+ <h3 class="section2">Merging Mappings</h3>
1111
+ <p>YAML allows user to merge mappings.
1112
+ </p>
1113
+ <pre class="program">- <strong>&amp;a1</strong>
1114
+ A: 10
1115
+ B: 20
1116
+ - <strong>&lt;&lt;: *a1</strong> # merge
1117
+ A: 15 # override
1118
+ C: 30 # add
1119
+ </pre>
1120
+ <p>This is equal to the following Ruby code.
1121
+ </p>
1122
+ <pre class="program">a1 = {"A"=&gt;10, "B"=&gt;20}
1123
+ tmp = {}
1124
+ tmp.update(a1) # merge
1125
+ tmp["A"] = 15 # override
1126
+ tmp["C"] = 30 # add
1127
+ </pre>
1128
+ <p>This feature allows Kwalify to merge rules.
1129
+ </p>
1130
+ <a name="schema15.yaml"></a>
1131
+ <div class="program_caption">
1132
+ <code>schema15.yaml</code> : default rule example</div>
1133
+ <pre class="program">type: map
1134
+ mapping:
1135
+ "group":
1136
+ type: map
1137
+ mapping:
1138
+ "name": <strong>&amp;name</strong>
1139
+ type: str
1140
+ required: yes
1141
+ "email": <strong>&amp;email</strong>
1142
+ type: str
1143
+ pattern: /@/
1144
+ required: no
1145
+ "user":
1146
+ type: map
1147
+ mapping:
1148
+ "name":
1149
+ <strong>&lt;&lt;: *name # merge</strong>
1150
+ <strong>length: { max: 16 } # override</strong>
1151
+ "email":
1152
+ <strong>&lt;&lt;: *email # merge</strong>
1153
+ <strong>required: yes # add</strong>
1154
+ </pre>
1155
+ <a name="document15a.yaml"></a>
1156
+ <div class="program_caption">
1157
+ <code>document15a.yaml</code> : valid document example</div>
1158
+ <pre class="program">group:
1159
+ name: foo
1160
+ email: foo@mail.com
1161
+ user:
1162
+ name: bar
1163
+ email: bar@mail.com
1164
+ </pre>
1165
+ <div class="terminal_caption">
1166
+ validate</div>
1167
+ <pre class="terminal">$ kwalify -lf schema15.yaml document15a.yaml
1168
+ document15a.yaml#0: valid.
1169
+ </pre>
1170
+ <a name="document15b.yaml"></a>
1171
+ <div class="program_caption">
1172
+ <code>document15b.yaml</code> : invalid document example</div>
1173
+ <pre class="program">group:
1174
+ name: foo
1175
+ email: foo@mail.com
1176
+ user:
1177
+ name: toooooo-looooong-name
1178
+ </pre>
1179
+ <div class="terminal_caption">
1180
+ validate</div>
1181
+ <pre class="terminal">$ kwalify -lf schema15.yaml document15b.yaml
1182
+ document15b.yaml#0: INVALID
1183
+ - (line 4) [/user] key 'email:' is required.
1184
+ - (line 5) [/user/name] 'toooooo-looooong-name': too long (length 21 &gt; max 16).
919
1185
  </pre>
920
1186
  <br>
921
1187