kwalify 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +232 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +16 -19
- data/bin/kwalify +3 -3
- data/contrib/inline-require +6 -4
- data/contrib/kwalify +3719 -2427
- data/doc-api/classes/CommandOptionError.html +17 -17
- data/doc-api/classes/CommandOptionParser.html +63 -63
- data/doc-api/classes/Kwalify.html +29 -7
- data/doc-api/classes/Kwalify/AssertionError.html +9 -9
- data/doc-api/classes/Kwalify/BaseError.html +72 -71
- data/doc-api/classes/Kwalify/BaseParser.html +461 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
- data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
- data/doc-api/classes/Kwalify/HashInterface.html +13 -135
- data/doc-api/classes/Kwalify/Json.html +105 -0
- data/doc-api/classes/Kwalify/Main.html +129 -126
- data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
- data/doc-api/classes/Kwalify/Parser.html +12 -12
- data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
- data/doc-api/classes/Kwalify/Rule.html +152 -130
- data/doc-api/classes/Kwalify/SchemaError.html +10 -10
- data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
- data/doc-api/classes/Kwalify/Types.html +26 -25
- data/doc-api/classes/Kwalify/Util.html +389 -0
- data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
- data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
- data/doc-api/classes/Kwalify/ValidationError.html +10 -10
- data/doc-api/classes/Kwalify/Validator.html +153 -86
- data/doc-api/classes/Kwalify/Yaml.html +181 -0
- data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
- data/doc-api/classes/Kwalify/YamlParser.html +190 -183
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +17 -22
- data/doc-api/files/kwalify/errors_rb.html +2 -2
- data/doc-api/files/kwalify/main_rb.html +4 -3
- data/doc-api/files/kwalify/messages_rb.html +2 -2
- data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
- data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
- data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +2 -2
- data/doc-api/files/kwalify/types_rb.html +2 -2
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
- data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
- data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
- data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
- data/doc-api/files/kwalify/util_rb.html +107 -0
- data/doc-api/files/kwalify/validator_rb.html +2 -2
- data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
- data/doc-api/files/kwalify_rb.html +3 -2
- data/doc-api/fr_class_index.html +8 -1
- data/doc-api/fr_file_index.html +5 -1
- data/doc-api/fr_method_index.html +128 -69
- data/doc/img/fig01.png +0 -0
- data/doc/users-guide.html +882 -717
- data/examples/address-book/address-book.schema.yaml +2 -2
- data/examples/data-binding/BABEL.data.yaml +63 -0
- data/examples/data-binding/BABEL.schema.yaml +31 -0
- data/examples/data-binding/Makefile +8 -0
- data/examples/data-binding/Rakefile +13 -0
- data/examples/data-binding/main.rb +27 -0
- data/examples/invoice/invoice.schema.yaml +3 -3
- data/examples/tapkit/tapkit.schema.yaml +2 -2
- data/lib/kwalify.rb +41 -4
- data/lib/kwalify/errors.rb +118 -96
- data/lib/kwalify/kwalify.schema.yaml +58 -0
- data/lib/kwalify/main.rb +384 -377
- data/lib/kwalify/messages.rb +41 -27
- data/lib/kwalify/meta-validator.rb +251 -331
- data/lib/kwalify/parser/base.rb +127 -0
- data/lib/kwalify/parser/yaml.rb +837 -0
- data/lib/kwalify/rule.rb +545 -487
- data/lib/kwalify/templates/genclass-java.eruby +189 -162
- data/lib/kwalify/templates/genclass-php.eruby +104 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
- data/lib/kwalify/types.rb +107 -106
- data/lib/kwalify/util.rb +157 -0
- data/lib/kwalify/util/assert-text-equal.rb +33 -31
- data/lib/kwalify/util/hash-interface.rb +11 -30
- data/lib/kwalify/util/hashlike.rb +51 -0
- data/lib/kwalify/util/option-parser.rb +144 -144
- data/lib/kwalify/util/ordered-hash.rb +57 -0
- data/lib/kwalify/util/testcase-helper.rb +3 -3
- data/lib/kwalify/validator.rb +267 -212
- data/lib/kwalify/yaml-parser.rb +822 -768
- data/setup.rb +861 -607
- data/test/Rookbook.yaml +10 -0
- data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
- data/test/data/users-guide/BABEL.data.yaml +24 -0
- data/test/data/users-guide/BABEL.schema.yaml +30 -0
- data/test/data/users-guide/ExampleAddressBook.java +47 -0
- data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
- data/test/data/users-guide/Person.java.expected +44 -0
- data/test/data/users-guide/address_book.rb +52 -0
- data/test/data/users-guide/address_book.schema.yaml +28 -0
- data/test/data/users-guide/address_book.yaml +27 -0
- data/test/data/users-guide/answers-schema.yaml +12 -0
- data/test/data/users-guide/answers-validator.rb +52 -0
- data/test/data/users-guide/babel_genclass.result +26 -0
- data/test/data/users-guide/config.schema.yaml +7 -0
- data/test/data/users-guide/config.yaml +4 -0
- data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
- data/test/data/users-guide/document01b.yaml +3 -0
- data/test/data/users-guide/document02a.yaml +4 -0
- data/test/data/users-guide/document02b.yaml +4 -0
- data/test/data/users-guide/document03a.yaml +6 -0
- data/test/data/users-guide/document03b.yaml +6 -0
- data/test/data/users-guide/document04a.yaml +9 -0
- data/test/data/users-guide/document04b.yaml +9 -0
- data/test/data/users-guide/document05a.yaml +11 -0
- data/test/data/users-guide/document05b.yaml +12 -0
- data/test/data/users-guide/document06a.yaml +15 -0
- data/test/data/users-guide/document06b.yaml +16 -0
- data/test/data/users-guide/document07a.yaml +9 -0
- data/test/data/users-guide/document07b.yaml +7 -0
- data/test/data/users-guide/document12a.json +10 -0
- data/test/data/users-guide/document12b.json +6 -0
- data/test/data/users-guide/document13a.yaml +17 -0
- data/test/data/users-guide/document14a.yaml +3 -0
- data/test/data/users-guide/document14b.yaml +3 -0
- data/test/data/users-guide/document15a.yaml +6 -0
- data/test/data/users-guide/document15b.yaml +5 -0
- data/test/data/users-guide/example_address_book.rb +10 -0
- data/test/data/users-guide/example_address_book_java.result +32 -0
- data/test/data/users-guide/example_address_book_ruby.result +31 -0
- data/test/data/users-guide/genclass_java.result +4 -0
- data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
- data/test/data/users-guide/howto-validation.rb +25 -0
- data/test/data/users-guide/howto3.rb +6 -0
- data/test/data/users-guide/howto3.result +5 -0
- data/test/data/users-guide/howto3.yaml +8 -0
- data/test/data/users-guide/howto5_databinding.result +111 -0
- data/test/data/users-guide/invalid01.result +3 -0
- data/test/data/users-guide/invalid02.result +5 -0
- data/test/data/users-guide/invalid03.result +5 -0
- data/test/data/users-guide/invalid04.result +4 -0
- data/test/data/users-guide/invalid05.result +11 -0
- data/test/data/users-guide/invalid06.result +4 -0
- data/test/data/users-guide/invalid07.result +3 -0
- data/test/data/users-guide/invalid08.result +3 -0
- data/test/data/users-guide/invalid12.json +8 -0
- data/test/data/users-guide/invalid14.result +4 -0
- data/test/data/users-guide/invalid15.result +4 -0
- data/test/data/users-guide/loadbabel.rb +27 -0
- data/test/data/users-guide/loadconfig.rb +15 -0
- data/test/data/users-guide/loadconfig.result +2 -0
- data/test/data/users-guide/models.rb +22 -0
- data/test/data/users-guide/option_ha.result +6 -0
- data/test/data/users-guide/option_ha_genclass_java.result +7 -0
- data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
- data/test/data/users-guide/schema02.yaml +12 -0
- data/test/data/users-guide/schema03.yaml +9 -0
- data/test/data/users-guide/schema04.yaml +20 -0
- data/test/data/users-guide/schema05.yaml +29 -0
- data/test/data/users-guide/schema06.yaml +11 -0
- data/test/data/users-guide/schema12.json +12 -0
- data/test/data/users-guide/schema13.yaml +13 -0
- data/test/data/users-guide/schema14.yaml +5 -0
- data/test/data/users-guide/schema15.yaml +21 -0
- data/test/data/users-guide/valid01.result +2 -0
- data/test/data/users-guide/valid02.result +2 -0
- data/test/data/users-guide/valid03.result +2 -0
- data/test/data/users-guide/valid04.result +2 -0
- data/test/data/users-guide/valid05.result +2 -0
- data/test/data/users-guide/valid06.result +2 -0
- data/test/data/users-guide/valid07.result +2 -0
- data/test/data/users-guide/valid08.result +2 -0
- data/test/data/users-guide/valid12.result +2 -0
- data/test/data/users-guide/valid13.result +2 -0
- data/test/data/users-guide/valid14.result +2 -0
- data/test/data/users-guide/valid15.result +2 -0
- data/test/data/users-guide/validate08.rb +37 -0
- data/test/test-action.rb +78 -0
- data/test/test-action.yaml +738 -0
- data/test/test-databinding.rb +80 -0
- data/test/test-databinding.yaml +301 -0
- data/test/test-main.rb +129 -150
- data/test/test-main.yaml +126 -321
- data/test/test-metavalidator.rb +47 -47
- data/test/test-metavalidator.yaml +77 -21
- data/test/test-parser-yaml.rb +57 -0
- data/test/test-parser-yaml.yaml +1749 -0
- data/test/test-rule.rb +14 -15
- data/test/test-rule.yaml +6 -3
- data/test/test-users-guide.rb +75 -0
- data/test/test-validator.rb +77 -52
- data/test/test-validator.yaml +168 -6
- data/test/test-yaml-parser.rb +47 -0
- data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
- data/test/test.rb +37 -21
- metadata +136 -37
- data/COPYING +0 -340
- data/ChangeLog +0 -70
- data/doc-api/classes/YamlHelper.html +0 -259
- data/lib/kwalify/util/yaml-helper.rb +0 -82
- data/test/test-yamlparser.rb +0 -58
- data/test/tmp.dir/User.java +0 -43
- data/test/tmp.dir/action1.document +0 -18
- data/test/tmp.dir/action1.schema +0 -32
- data/test/tmp.dir/action2.document +0 -18
- data/test/tmp.dir/action2.schema +0 -32
- data/test/tmp.dir/emacs.document +0 -6
- data/test/tmp.dir/emacs.schema +0 -6
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +0 -3
- data/test/tmp.dir/silent1.schema +0 -3
- data/test/tmp.dir/silent2.document +0 -7
- data/test/tmp.dir/silent2.schema +0 -3
- data/test/tmp.dir/stream.invalid +0 -8
- data/test/tmp.dir/stream.schema +0 -3
- data/test/tmp.dir/stream.valid +0 -8
- data/test/tmp.dir/untabify.document +0 -5
- data/test/tmp.dir/untabify.schema +0 -10
- data/todo.txt +0 -34
data/test/Rookbook.yaml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
|
2
|
+
recipes:
|
3
|
+
|
4
|
+
- product: :diff
|
5
|
+
ingreds: [ test-parser-yaml.yaml, test-yaml-parser.yaml ]
|
6
|
+
byprods: [ hoge.yaml, hoge.diff ]
|
7
|
+
method*: |
|
8
|
+
sys "ruby -e 's=ARGF.read(); print s.gsub(/^locations:.*?(?=^\\S)/m,\"\").gsub(/Kwalify::SyntaxError/,\"Kwalify::YamlSyntaxError\")' #{@ingreds[0]} > #{@byprods[0]}"
|
9
|
+
sys! "diff -U5 hoge.yaml #{@ingreds[1]} | tee #{@byprods[1]}"
|
10
|
+
rm @byprods
|
@@ -1,18 +1,18 @@
|
|
1
|
-
// generated by kwalify from
|
1
|
+
// generated by kwalify from address_book.schema.yaml
|
2
2
|
|
3
3
|
import java.util.*;
|
4
4
|
|
5
5
|
/**
|
6
|
-
*
|
6
|
+
* address-book class
|
7
7
|
*/
|
8
|
-
public class
|
8
|
+
public class AddressBook {
|
9
9
|
|
10
10
|
private List _groups;
|
11
|
-
private List
|
11
|
+
private List _people;
|
12
12
|
|
13
|
-
public
|
13
|
+
public AddressBook() {}
|
14
14
|
|
15
|
-
public
|
15
|
+
public AddressBook(Map map) {
|
16
16
|
List seq;
|
17
17
|
Object obj;
|
18
18
|
if ((seq = (List)map.get("groups")) != null) {
|
@@ -23,18 +23,18 @@ public class Context {
|
|
23
23
|
}
|
24
24
|
}
|
25
25
|
_groups = seq;
|
26
|
-
if ((seq = (List)map.get("
|
26
|
+
if ((seq = (List)map.get("people")) != null) {
|
27
27
|
for (int i = 0; i < seq.size(); i++) {
|
28
28
|
if ((obj = seq.get(i)) instanceof Map) {
|
29
|
-
seq.set(i, new
|
29
|
+
seq.set(i, new Person((Map)obj));
|
30
30
|
}
|
31
31
|
}
|
32
32
|
}
|
33
|
-
|
33
|
+
_people = seq;
|
34
34
|
}
|
35
35
|
|
36
36
|
public List getGroups() { return _groups; }
|
37
37
|
public void setGroups(List groups_) { _groups = groups_; }
|
38
|
-
public List
|
39
|
-
public void
|
38
|
+
public List getPeople() { return _people; }
|
39
|
+
public void setPeople(List people_) { _people = people_; }
|
40
40
|
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
teams:
|
2
|
+
- &thechildren
|
3
|
+
name: The Children
|
4
|
+
desc: Level 7 ESPers
|
5
|
+
chief: *minamoto # preceding alias
|
6
|
+
members: [*kaoru, *aoi, *shiho] # preceding aliases
|
7
|
+
|
8
|
+
members:
|
9
|
+
- &minamoto
|
10
|
+
name: Kohichi Minamoto
|
11
|
+
desc: Scientist
|
12
|
+
team: *thechildren
|
13
|
+
- &kaoru
|
14
|
+
name: Kaoru Akashi
|
15
|
+
desc: Psychokino
|
16
|
+
team: *thechildren
|
17
|
+
- &aoi
|
18
|
+
name: Aoi Nogami
|
19
|
+
desc: Teleporter
|
20
|
+
team: *thechildren
|
21
|
+
- &shiho
|
22
|
+
name: Shiho Sannomiya
|
23
|
+
desc: Psycometrer
|
24
|
+
team: *thechildren
|
@@ -0,0 +1,30 @@
|
|
1
|
+
type: map
|
2
|
+
required: yes
|
3
|
+
mapping:
|
4
|
+
"teams":
|
5
|
+
type: seq
|
6
|
+
required: yes
|
7
|
+
sequence:
|
8
|
+
- &team
|
9
|
+
type: map
|
10
|
+
required: yes
|
11
|
+
class: Team
|
12
|
+
mapping:
|
13
|
+
"name": {type: str, required: yes, unique: yes}
|
14
|
+
"desc": {type: str}
|
15
|
+
"chief": *member # preceding alias
|
16
|
+
"members":
|
17
|
+
type: seq
|
18
|
+
sequence: [*member] # preceding alias
|
19
|
+
"members":
|
20
|
+
type: seq
|
21
|
+
required: yes
|
22
|
+
sequence:
|
23
|
+
- &member
|
24
|
+
type: map
|
25
|
+
required: yes
|
26
|
+
class: Member
|
27
|
+
mapping:
|
28
|
+
"name": {type: str, required: yes, unique: yes}
|
29
|
+
"desc": {type: str}
|
30
|
+
"team": *team
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import java.util.*;
|
2
|
+
import kwalify.*;
|
3
|
+
|
4
|
+
public class ExampleAddressBook {
|
5
|
+
public static void main(String args[]) throws Exception {
|
6
|
+
// read schema
|
7
|
+
String schema_str = Util.readFile("address_book.schema.yaml");
|
8
|
+
schema_str = Util.untabify(schema_str);
|
9
|
+
Object schema = new YamlParser(schema_str).parse();
|
10
|
+
|
11
|
+
// read document file
|
12
|
+
String document_str = Util.readFile("address_book.yaml");
|
13
|
+
document_str = Util.untabify(document_str);
|
14
|
+
YamlParser parser = new YamlParser(document_str);
|
15
|
+
Object document = parser.parse();
|
16
|
+
|
17
|
+
// create address book object
|
18
|
+
AddressBook addrbook = new AddressBook((Map)document);
|
19
|
+
|
20
|
+
// show groups
|
21
|
+
List groups = addrbook.getGroups();
|
22
|
+
if (groups != null) {
|
23
|
+
for (Iterator it = groups.iterator(); it.hasNext(); ) {
|
24
|
+
Group group = (Group)it.next();
|
25
|
+
System.out.println("group name: " + group.getName());
|
26
|
+
System.out.println("group desc: " + group.getDesc());
|
27
|
+
System.out.println();
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
// show people
|
32
|
+
List people = addrbook.getPeople();
|
33
|
+
if (people != null) {
|
34
|
+
for (Iterator it = people.iterator(); it.hasNext(); ) {
|
35
|
+
Person person = (Person)it.next();
|
36
|
+
System.out.println("person name: " + person.getName());
|
37
|
+
System.out.println("person group: " + person.getGroup());
|
38
|
+
System.out.println("person email: " + person.getEmail());
|
39
|
+
System.out.println("person phone: " + person.getPhone());
|
40
|
+
System.out.println("person blood: " + person.getBlood());
|
41
|
+
System.out.println("person birth: " + person.getBirth());
|
42
|
+
System.out.println();
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
@@ -1,33 +1,24 @@
|
|
1
|
-
// generated by kwalify from
|
1
|
+
// generated by kwalify from address_book.schema.yaml
|
2
2
|
|
3
3
|
import java.util.*;
|
4
4
|
|
5
5
|
/**
|
6
|
-
*
|
6
|
+
* group class
|
7
7
|
*/
|
8
8
|
public class Group {
|
9
9
|
|
10
10
|
private String _name;
|
11
11
|
private String _desc;
|
12
|
-
private User _owner;
|
13
12
|
|
14
13
|
public Group() {}
|
15
14
|
|
16
15
|
public Group(Map map) {
|
17
16
|
_name = (String)map.get("name");
|
18
17
|
_desc = (String)map.get("desc");
|
19
|
-
Object obj;
|
20
|
-
if ((obj = map.get("owner")) != null && obj instanceof Map) {
|
21
|
-
_owner = new User((Map)obj);
|
22
|
-
} else {
|
23
|
-
_owner = (User)obj;
|
24
|
-
}
|
25
18
|
}
|
26
19
|
|
27
20
|
public String getName() { return _name; }
|
28
21
|
public void setName(String name_) { _name = name_; }
|
29
22
|
public String getDesc() { return _desc; }
|
30
23
|
public void setDesc(String desc_) { _desc = desc_; }
|
31
|
-
public User getOwner() { return _owner; }
|
32
|
-
public void setOwner(User owner_) { _owner = owner_; }
|
33
24
|
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
// generated by kwalify from address_book.schema.yaml
|
2
|
+
|
3
|
+
import java.util.*;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* person class
|
7
|
+
*/
|
8
|
+
public class Person {
|
9
|
+
|
10
|
+
private String _name;
|
11
|
+
private String _desc;
|
12
|
+
private String _group;
|
13
|
+
private String _email;
|
14
|
+
private String _phone;
|
15
|
+
private Date _birth;
|
16
|
+
private String _blood;
|
17
|
+
|
18
|
+
public Person() {}
|
19
|
+
|
20
|
+
public Person(Map map) {
|
21
|
+
_name = (String)map.get("name");
|
22
|
+
_desc = (String)map.get("desc");
|
23
|
+
_group = (String)map.get("group");
|
24
|
+
_email = (String)map.get("email");
|
25
|
+
_phone = (String)map.get("phone");
|
26
|
+
_birth = (Date)map.get("birth");
|
27
|
+
_blood = (String)map.get("blood");
|
28
|
+
}
|
29
|
+
|
30
|
+
public String getName() { return _name; }
|
31
|
+
public void setName(String name_) { _name = name_; }
|
32
|
+
public String getDesc() { return _desc; }
|
33
|
+
public void setDesc(String desc_) { _desc = desc_; }
|
34
|
+
public String getGroup() { return _group; }
|
35
|
+
public void setGroup(String group_) { _group = group_; }
|
36
|
+
public String getEmail() { return _email; }
|
37
|
+
public void setEmail(String email_) { _email = email_; }
|
38
|
+
public String getPhone() { return _phone; }
|
39
|
+
public void setPhone(String phone_) { _phone = phone_; }
|
40
|
+
public Date getBirth() { return _birth; }
|
41
|
+
public void setBirth(Date birth_) { _birth = birth_; }
|
42
|
+
public String getBlood() { return _blood; }
|
43
|
+
public void setBlood(String blood_) { _blood = blood_; }
|
44
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
## address-book class
|
2
|
+
class AddressBook
|
3
|
+
def initialize(hash=nil)
|
4
|
+
if hash.nil?
|
5
|
+
return
|
6
|
+
end
|
7
|
+
@groups = (v=hash['groups']) ? v.map!{|e| e.is_a?(Group) ? e : Group.new(e)} : v
|
8
|
+
@people = (v=hash['people']) ? v.map!{|e| e.is_a?(Person) ? e : Person.new(e)} : v
|
9
|
+
end
|
10
|
+
attr_accessor :groups # seq
|
11
|
+
attr_accessor :people # seq
|
12
|
+
end
|
13
|
+
|
14
|
+
## group class
|
15
|
+
class Group
|
16
|
+
def initialize(hash=nil)
|
17
|
+
if hash.nil?
|
18
|
+
return
|
19
|
+
end
|
20
|
+
@name = hash['name']
|
21
|
+
@desc = hash['desc']
|
22
|
+
end
|
23
|
+
attr_accessor :name # str
|
24
|
+
attr_accessor :desc # str
|
25
|
+
end
|
26
|
+
|
27
|
+
## person class
|
28
|
+
class Person
|
29
|
+
def initialize(hash=nil)
|
30
|
+
if hash.nil?
|
31
|
+
@deleted = false
|
32
|
+
return
|
33
|
+
end
|
34
|
+
@name = hash['name']
|
35
|
+
@desc = hash['desc']
|
36
|
+
@group = hash['group']
|
37
|
+
@email = hash['email']
|
38
|
+
@phone = hash['phone']
|
39
|
+
@birth = hash['birth']
|
40
|
+
@blood = hash['blood']
|
41
|
+
@deleted = (v=hash['deleted']).nil? ? false : v
|
42
|
+
end
|
43
|
+
attr_accessor :name # str
|
44
|
+
attr_accessor :desc # str
|
45
|
+
attr_accessor :group # str
|
46
|
+
attr_accessor :email # str
|
47
|
+
attr_accessor :phone # str
|
48
|
+
attr_accessor :birth # date
|
49
|
+
attr_accessor :blood # str
|
50
|
+
attr_accessor :deleted # bool
|
51
|
+
def deleted? ; @deleted ; end
|
52
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
type: map
|
2
|
+
class: AddressBook
|
3
|
+
desc: address-book class
|
4
|
+
mapping:
|
5
|
+
"groups":
|
6
|
+
type: seq
|
7
|
+
sequence:
|
8
|
+
- type: map
|
9
|
+
class: Group
|
10
|
+
desc: group class
|
11
|
+
mapping:
|
12
|
+
"name": { type: str, required: yes }
|
13
|
+
"desc": { type: str }
|
14
|
+
"people":
|
15
|
+
type: seq
|
16
|
+
sequence:
|
17
|
+
- type: map
|
18
|
+
class: Person
|
19
|
+
desc: person class
|
20
|
+
mapping:
|
21
|
+
"name": { type: str, required: yes }
|
22
|
+
"desc": { type: str }
|
23
|
+
"group": { type: str }
|
24
|
+
"email": { type: str, pattern: '/@/' }
|
25
|
+
"phone": { type: str }
|
26
|
+
"birth": { type: date }
|
27
|
+
"blood": { type: str, enum: [A, B, O, AB] }
|
28
|
+
"deleted": { type: bool, default: false }
|
@@ -0,0 +1,27 @@
|
|
1
|
+
groups:
|
2
|
+
|
3
|
+
- name: family
|
4
|
+
desc: my family
|
5
|
+
|
6
|
+
- name: friend
|
7
|
+
desc: my friends
|
8
|
+
|
9
|
+
- name: business
|
10
|
+
desc: those who works together
|
11
|
+
|
12
|
+
people:
|
13
|
+
|
14
|
+
- name: Sumire
|
15
|
+
group: family
|
16
|
+
birth: 2000-01-01
|
17
|
+
blood: A
|
18
|
+
|
19
|
+
- name: Shiina
|
20
|
+
group: friend
|
21
|
+
birth: 1995-01-01
|
22
|
+
email: shiina@mail.org
|
23
|
+
|
24
|
+
- name: Sakura
|
25
|
+
group: business
|
26
|
+
email: cherry@mail.net
|
27
|
+
phone: 012-345-6789
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'kwalify'
|
4
|
+
|
5
|
+
## validator class for answers
|
6
|
+
class AnswersValidator < Kwalify::Validator
|
7
|
+
|
8
|
+
## load schema definition
|
9
|
+
@@schema = Kwalify::Yaml.load_file('answers-schema.yaml')
|
10
|
+
## or
|
11
|
+
## require 'yaml'
|
12
|
+
## @@schema = YAML.load_file('answers-schema.yaml')
|
13
|
+
|
14
|
+
def initialize()
|
15
|
+
super(@@schema)
|
16
|
+
end
|
17
|
+
|
18
|
+
## hook method called by Validator#validate()
|
19
|
+
def validate_hook(value, rule, path, errors)
|
20
|
+
case rule.name
|
21
|
+
when 'Answer'
|
22
|
+
if value['answer'] == 'bad'
|
23
|
+
reason = value['reason']
|
24
|
+
if !reason || reason.empty?
|
25
|
+
msg = "reason is required when answer is 'bad'."
|
26
|
+
errors << Kwalify::ValidationError.new(msg, path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
## create validator
|
35
|
+
validator = AnswersValidator.new
|
36
|
+
|
37
|
+
## load YAML document
|
38
|
+
input = ARGF.read()
|
39
|
+
document = Kwalify::Yaml.load(input)
|
40
|
+
## or document = YAML.load(input)
|
41
|
+
|
42
|
+
## validate
|
43
|
+
errors = validator.validate(document)
|
44
|
+
if errors.empty?
|
45
|
+
puts "Valid."
|
46
|
+
else
|
47
|
+
puts "*** INVALID!"
|
48
|
+
errors.each do |error|
|
49
|
+
# error.class == Kwalify::ValidationError
|
50
|
+
puts " - [#{error.path}] : #{error.message}"
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$ kwalify -a genclass-ruby -P -f BABEL.schema.yaml \
|
2
|
+
--hashlike --initialize=false --module=Babel
|
3
|
+
require 'kwalify/util/hashlike'
|
4
|
+
|
5
|
+
module Babel
|
6
|
+
|
7
|
+
##
|
8
|
+
class Team
|
9
|
+
include Kwalify::Util::HashLike
|
10
|
+
attr_accessor :name # str
|
11
|
+
attr_accessor :desc # str
|
12
|
+
attr_accessor :chief # map
|
13
|
+
attr_accessor :members # seq
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
class Member
|
18
|
+
include Kwalify::Util::HashLike
|
19
|
+
attr_accessor :name # str
|
20
|
+
attr_accessor :desc # str
|
21
|
+
attr_accessor :team # map
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
$ kwalify -a genclass-ruby -P -f BABEL.schema.yaml \
|
26
|
+
--hashlike --initialize=false --module=Babel > models.rb
|