kwalify 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +29 -62
- data/README.txt +39 -11
- data/bin/kwalify +16 -177
- data/doc/users-guide.html +279 -145
- data/examples/address-book/Makefile +1 -0
- data/examples/address-book/address-book.schema.yaml +10 -10
- data/examples/invoice/Makefile +1 -0
- data/examples/invoice/invoice.schema.yaml +17 -34
- data/examples/tapkit/Makefile +5 -0
- data/examples/tapkit/tapkit.schema.yaml +137 -0
- data/examples/tapkit/tapkit.yaml +85 -0
- data/lib/kwalify.rb +12 -3
- data/lib/kwalify/errors.rb +36 -60
- data/lib/kwalify/main-program.rb +214 -0
- data/lib/kwalify/messages.rb +91 -0
- data/lib/kwalify/meta-validator.rb +199 -76
- data/lib/kwalify/rule.rb +326 -0
- data/lib/kwalify/types.rb +86 -20
- data/lib/kwalify/util/assert-diff.rb +1 -1
- data/lib/kwalify/util/option-parser.rb +1 -1
- data/lib/kwalify/util/yaml-helper.rb +6 -6
- data/lib/kwalify/validator.rb +111 -218
- data/test/test-metavalidator.rb +608 -0
- data/test/test-metavalidator.rb.error +490 -0
- data/test/test-validator.rb +526 -0
- data/test/test.rb +12 -351
- data/todo.txt +14 -4
- metadata +12 -3
- data/lib/kwalify/error-msg.rb +0 -41
@@ -1,11 +1,11 @@
|
|
1
1
|
##
|
2
2
|
## Kwalify schema example for address book
|
3
3
|
##
|
4
|
-
## $Release: 0.
|
4
|
+
## $Release: 0.2.0 $
|
5
5
|
## copyright(c) 2005 kuwata-lab all rights reserved.
|
6
6
|
##
|
7
7
|
##
|
8
|
-
## NOTE: 'type:
|
8
|
+
## NOTE: 'type: str' is omitted in this example because 'str' is default type.
|
9
9
|
##
|
10
10
|
|
11
11
|
type: seq
|
@@ -15,29 +15,29 @@ sequence:
|
|
15
15
|
required: yes
|
16
16
|
mapping:
|
17
17
|
"name":
|
18
|
-
|
18
|
+
type: str
|
19
19
|
required: yes
|
20
20
|
"email":
|
21
|
-
|
21
|
+
type: str
|
22
22
|
pattern: /@/
|
23
23
|
"tel":
|
24
|
-
|
24
|
+
type: str
|
25
25
|
pattern: /^\d+-\d+-\d+$/
|
26
26
|
"birth":
|
27
27
|
type: date
|
28
28
|
"age":
|
29
|
-
type:
|
29
|
+
type: int
|
30
30
|
"zip":
|
31
|
-
|
31
|
+
type: str
|
32
32
|
pattern: /^\d+-\d+$/
|
33
33
|
"addr":
|
34
|
-
|
34
|
+
type: str
|
35
35
|
"blood":
|
36
|
-
|
36
|
+
type: str
|
37
37
|
enum:
|
38
38
|
- A
|
39
39
|
- B
|
40
40
|
- O
|
41
41
|
- AB
|
42
42
|
"memo":
|
43
|
-
type:
|
43
|
+
type: any
|
data/examples/invoice/Makefile
CHANGED
@@ -1,37 +1,30 @@
|
|
1
1
|
###
|
2
2
|
### Kwalify schema example for invoice
|
3
3
|
###
|
4
|
-
### $Rev$
|
5
|
-
### $Release: 0.
|
4
|
+
### $Rev: 18 $
|
5
|
+
### $Release: 0.2.0 $
|
6
6
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
7
7
|
###
|
8
8
|
|
9
9
|
type: map
|
10
10
|
required: yes
|
11
11
|
mapping:
|
12
|
-
"invoice":
|
13
|
-
|
14
|
-
required: yes
|
15
|
-
"date":
|
16
|
-
type: date
|
17
|
-
required: yes
|
12
|
+
"invoice": { type: int, required: yes }
|
13
|
+
"date": { type: date, required: yes }
|
18
14
|
"bill-to": &customer
|
19
15
|
type: map
|
20
16
|
required: yes
|
21
17
|
mapping:
|
22
|
-
"given":
|
23
|
-
|
24
|
-
"family":
|
25
|
-
required: yes
|
18
|
+
"given": { type: str, required: yes }
|
19
|
+
"family": { type: str, required: yes }
|
26
20
|
"address":
|
27
21
|
type: map
|
28
22
|
required: yes
|
29
23
|
mapping:
|
30
|
-
"lines":
|
31
|
-
"city":
|
32
|
-
"state":
|
33
|
-
"postal":
|
34
|
-
type: integer
|
24
|
+
"lines": { type: str }
|
25
|
+
"city": { type: str }
|
26
|
+
"state": { type: str }
|
27
|
+
"postal": { type: int }
|
35
28
|
"ship-to": *customer
|
36
29
|
"product":
|
37
30
|
type: seq
|
@@ -40,20 +33,10 @@ mapping:
|
|
40
33
|
- type: map
|
41
34
|
required: yes
|
42
35
|
mapping:
|
43
|
-
"sku":
|
44
|
-
|
45
|
-
|
46
|
-
"
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
"price":
|
51
|
-
type: float
|
52
|
-
"tax":
|
53
|
-
type: float
|
54
|
-
"total":
|
55
|
-
type: float
|
56
|
-
required: yes
|
57
|
-
"comments":
|
58
|
-
type: text
|
59
|
-
|
36
|
+
"sku": { type: str, required: yes, pattern: '/^[A-Z0-9]+$/' }
|
37
|
+
"quantity": { type: int, required: yes }
|
38
|
+
"description": { type: str }
|
39
|
+
"price": { type: float }
|
40
|
+
"tax": { type: float }
|
41
|
+
"total": { type: float, required: yes }
|
42
|
+
"comments": { type: str }
|
@@ -0,0 +1,137 @@
|
|
1
|
+
##
|
2
|
+
## Kwalify schema example for TapKit
|
3
|
+
##
|
4
|
+
## $Release: 0.2.0 $
|
5
|
+
## copyright(c) 2005 kuwata-lab all rights reserved.
|
6
|
+
##
|
7
|
+
|
8
|
+
type: map
|
9
|
+
mapping:
|
10
|
+
"adapter_name":
|
11
|
+
type: str
|
12
|
+
enum:
|
13
|
+
- DBI
|
14
|
+
- CSV
|
15
|
+
#- MySQL
|
16
|
+
#- PostgreSQL
|
17
|
+
#- OpenBase
|
18
|
+
"connection":
|
19
|
+
type: map
|
20
|
+
mapping:
|
21
|
+
"url":
|
22
|
+
type: str
|
23
|
+
required: yes
|
24
|
+
"user":
|
25
|
+
type: str
|
26
|
+
"password":
|
27
|
+
type: str
|
28
|
+
"encoding":
|
29
|
+
type: str
|
30
|
+
enum:
|
31
|
+
- jis
|
32
|
+
- sjis
|
33
|
+
- euc
|
34
|
+
"path":
|
35
|
+
type: str
|
36
|
+
"entities":
|
37
|
+
type: seq
|
38
|
+
sequence:
|
39
|
+
- type: map
|
40
|
+
mapping:
|
41
|
+
"name":
|
42
|
+
type: str
|
43
|
+
required: yes
|
44
|
+
"external_name":
|
45
|
+
type: str
|
46
|
+
required: yes
|
47
|
+
"class_properties":
|
48
|
+
type: seq
|
49
|
+
required: yes
|
50
|
+
sequence:
|
51
|
+
- type: str
|
52
|
+
"primary_key_attributes":
|
53
|
+
type: seq
|
54
|
+
required: yes
|
55
|
+
sequence:
|
56
|
+
- type: str
|
57
|
+
"attributes":
|
58
|
+
type: seq
|
59
|
+
required: yes
|
60
|
+
sequence:
|
61
|
+
- type: map
|
62
|
+
mapping:
|
63
|
+
"name":
|
64
|
+
type: str
|
65
|
+
required: yes
|
66
|
+
"column_name":
|
67
|
+
type: text
|
68
|
+
required: yes
|
69
|
+
"class_name":
|
70
|
+
type: str
|
71
|
+
enum:
|
72
|
+
- String
|
73
|
+
- Integer
|
74
|
+
- Float
|
75
|
+
- Date
|
76
|
+
- Time
|
77
|
+
- Timestamp
|
78
|
+
- Boolean
|
79
|
+
"external_type":
|
80
|
+
type: str
|
81
|
+
required: yes
|
82
|
+
enum: [
|
83
|
+
char, character, varchar, char varying, character varying,
|
84
|
+
nchar, national char, national character,
|
85
|
+
national character varying, bit, bit varying,
|
86
|
+
int, integer, smallint, interval,
|
87
|
+
numeric, decimal, dec, float, real, double presicion,
|
88
|
+
date, time, timestamp
|
89
|
+
]
|
90
|
+
"allow_null":
|
91
|
+
type: bool
|
92
|
+
"read_only":
|
93
|
+
type: bool
|
94
|
+
"width":
|
95
|
+
type: int
|
96
|
+
"factory_method":
|
97
|
+
type: str
|
98
|
+
"convertion_method":
|
99
|
+
type: str
|
100
|
+
"relationships":
|
101
|
+
type: seq
|
102
|
+
sequence:
|
103
|
+
- type: map
|
104
|
+
mapping:
|
105
|
+
"name":
|
106
|
+
type: str
|
107
|
+
required: yes
|
108
|
+
"destination":
|
109
|
+
type: str
|
110
|
+
required: yes
|
111
|
+
"joins":
|
112
|
+
type: seq
|
113
|
+
required: yes
|
114
|
+
sequence:
|
115
|
+
- type: map
|
116
|
+
mapping:
|
117
|
+
"source":
|
118
|
+
type: str
|
119
|
+
"destination":
|
120
|
+
type: str
|
121
|
+
"join-semantic":
|
122
|
+
type: str
|
123
|
+
enum:
|
124
|
+
- inner
|
125
|
+
- left_outer
|
126
|
+
- right_outer
|
127
|
+
- full_outer
|
128
|
+
"to_many":
|
129
|
+
type: bool
|
130
|
+
"mandatory":
|
131
|
+
type: bool
|
132
|
+
"delete_rule":
|
133
|
+
type: str
|
134
|
+
enum:
|
135
|
+
- nullify
|
136
|
+
- cascade
|
137
|
+
- deny
|
@@ -0,0 +1,85 @@
|
|
1
|
+
##
|
2
|
+
## original:
|
3
|
+
## http://www.spice-of-life.net/tapkit/ja/TapKitUserGuide_J_c5_s6.html#doc7_1532
|
4
|
+
##
|
5
|
+
|
6
|
+
adapter_name: DBI
|
7
|
+
|
8
|
+
connection:
|
9
|
+
url: dbi:Mysql:examples
|
10
|
+
user: mysql
|
11
|
+
password: mysql
|
12
|
+
|
13
|
+
entities:
|
14
|
+
-
|
15
|
+
name: Employee
|
16
|
+
external_name: EMPLOYEE
|
17
|
+
|
18
|
+
attributes:
|
19
|
+
-
|
20
|
+
name: employee_id
|
21
|
+
column_name: EMPLOYEE_ID
|
22
|
+
external_type: int
|
23
|
+
class_name: Integer
|
24
|
+
-
|
25
|
+
name: name
|
26
|
+
column_name: NAME
|
27
|
+
external_type: char
|
28
|
+
class_name: String
|
29
|
+
width: 64
|
30
|
+
-
|
31
|
+
name: company_id
|
32
|
+
column_name: COMPANY_ID
|
33
|
+
external_type: int
|
34
|
+
class_name: Integer
|
35
|
+
|
36
|
+
relationships:
|
37
|
+
-
|
38
|
+
name: company
|
39
|
+
destination: Company
|
40
|
+
to_many: false
|
41
|
+
joins:
|
42
|
+
-
|
43
|
+
source: company_id
|
44
|
+
destination: company_id
|
45
|
+
|
46
|
+
primary_key_attributes:
|
47
|
+
- employee_id
|
48
|
+
|
49
|
+
class_properties:
|
50
|
+
- name
|
51
|
+
- company
|
52
|
+
|
53
|
+
-
|
54
|
+
name: Company
|
55
|
+
external_name: COMPANY
|
56
|
+
|
57
|
+
attributes:
|
58
|
+
-
|
59
|
+
name: company_id
|
60
|
+
column_name: COMPANY_ID
|
61
|
+
external_type: int
|
62
|
+
class_name: Integer
|
63
|
+
-
|
64
|
+
name: name
|
65
|
+
column_name: NAME
|
66
|
+
external_type: char
|
67
|
+
class_name: String
|
68
|
+
width: 64
|
69
|
+
|
70
|
+
relationships:
|
71
|
+
-
|
72
|
+
name: employees
|
73
|
+
destination: Employee
|
74
|
+
to_many: true
|
75
|
+
joins:
|
76
|
+
-
|
77
|
+
source: company_id
|
78
|
+
destination: company_id
|
79
|
+
|
80
|
+
primary_key_attributes:
|
81
|
+
- company_id
|
82
|
+
|
83
|
+
class_properties:
|
84
|
+
- name
|
85
|
+
- employees
|
data/lib/kwalify.rb
CHANGED
@@ -1,10 +1,19 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 18 $
|
3
|
+
### $Release: 0.2.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
|
+
|
8
|
+
module Kwalify
|
9
|
+
|
10
|
+
RELEASE = ("$Release: 0.2.0 $" =~ /[.\d]+/) && $&
|
11
|
+
|
12
|
+
end
|
13
|
+
|
7
14
|
require 'kwalify/types'
|
8
|
-
require 'kwalify/
|
15
|
+
require 'kwalify/messages'
|
9
16
|
require 'kwalify/errors'
|
17
|
+
require 'kwalify/rule'
|
10
18
|
require 'kwalify/validator'
|
19
|
+
require 'kwalify/meta-validator'
|
data/lib/kwalify/errors.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 18 $
|
3
|
+
### $Release: 0.2.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
|
+
require 'kwalify/messages'
|
8
|
+
|
7
9
|
module Kwalify
|
8
10
|
|
9
11
|
class KwalifyError < StandardError
|
@@ -18,95 +20,69 @@ module Kwalify
|
|
18
20
|
|
19
21
|
|
20
22
|
class BaseError < KwalifyError
|
21
|
-
def initialize(message="", path=nil,
|
23
|
+
def initialize(message="", path=nil, value=nil, rule=nil, error_symbol=nil)
|
22
24
|
super(message)
|
23
25
|
@error_symbol = error_symbol
|
24
|
-
@
|
26
|
+
@rule = rule
|
25
27
|
@path = path
|
28
|
+
@value = value
|
29
|
+
end
|
30
|
+
attr_reader :error_symbol, :rule, :path, :value
|
31
|
+
|
32
|
+
def path
|
33
|
+
return @path == '' ? "/" : @path
|
34
|
+
end
|
35
|
+
|
36
|
+
alias :_to_s :to_s
|
37
|
+
|
38
|
+
def message
|
39
|
+
_to_s
|
26
40
|
end
|
27
|
-
attr_reader :error_symbol, :schema, :path
|
28
41
|
|
29
42
|
def to_s
|
30
|
-
return
|
31
|
-
return "[/] #{super()}" if @path.empty?
|
32
|
-
return "[#{@path}] #{super()}"
|
43
|
+
return "[#{path()}] #{message()}"
|
33
44
|
end
|
34
45
|
end
|
35
46
|
|
36
47
|
|
37
48
|
class SchemaError < BaseError
|
38
|
-
def initialize(message="", path=nil,
|
39
|
-
super(message, path,
|
49
|
+
def initialize(message="", path=nil, rule=nil, value=nil, error_symbol=nil)
|
50
|
+
super(message, path, rule, value, error_symbol)
|
40
51
|
end
|
41
52
|
end
|
42
53
|
|
43
54
|
|
44
55
|
class ValidationError < BaseError
|
45
|
-
def initialize(message="", path=nil,
|
46
|
-
super(message, path,
|
47
|
-
@data = data
|
56
|
+
def initialize(message="", path=nil, rule=nil, value=nil, error_symbol=nil)
|
57
|
+
super(message, path, rule, value, error_symbol)
|
48
58
|
end
|
49
|
-
attr_reader :data
|
50
59
|
end
|
51
60
|
|
52
61
|
|
53
|
-
module
|
62
|
+
module ErrorHelper
|
54
63
|
|
55
64
|
def assert_error(message="")
|
56
65
|
raise AssertionError.new(message)
|
57
66
|
end
|
58
67
|
|
59
|
-
|
60
|
-
|
61
|
-
msg = nil
|
62
|
-
case error_symbol
|
63
|
-
|
64
|
-
## validation error
|
65
|
-
when :missing_value
|
66
|
-
msg = ERROR_MESSAGES[error_symbol]
|
67
|
-
when :invalid_type
|
68
|
-
msg = ERROR_MESSAGES[error_symbol] % [ schema.type, data.class.name ]
|
69
|
-
when :invalid_enum
|
70
|
-
msg = ERROR_MESSAGES[error_symbol] % [ data.to_s ]
|
71
|
-
when :invalid_pattern
|
72
|
-
msg = ERROR_MESSAGES[error_symbol] % [ data.to_s, schema.pattern.inspect ]
|
73
|
-
when :missing_key
|
74
|
-
msg = ERROR_MESSAGES[error_symbol] % [ arg ] # arg is keyname
|
75
|
-
when :invalid_key
|
76
|
-
msg = ERROR_MESSAGES[error_symbol] % [ arg ] # arg is keyname
|
77
|
-
|
78
|
-
## schema error
|
79
|
-
when :unexpected_key
|
80
|
-
msg = ERROR_MESSAGES[error_symbol] % [ arg ] # arg is keyname
|
81
|
-
when :duplicate_key
|
82
|
-
msg = ERROR_MESSAGES[error_symbol] % [ arg ] # arg is keyname
|
83
|
-
when :type_not_found
|
84
|
-
msg = ERROR_MESSAGES[error_symbol] % [ arg ] # arg is typename
|
85
|
-
when :regexp_error
|
86
|
-
msg = ERROR_MESSAGES[error_symbol] % [ data ]
|
87
|
-
else
|
88
|
-
msg = ERROR_MESSAGES[error_symbol]
|
89
|
-
end
|
90
|
-
|
91
|
-
##
|
92
|
-
return msg
|
68
|
+
def validate_error(error_symbol, rule, path, val, args=nil)
|
69
|
+
return _create_error(ValidationError, error_symbol, rule, path, val, args)
|
93
70
|
end
|
94
71
|
|
95
|
-
|
96
|
-
|
97
|
-
msg = error_message(error_symbol, schema, data, arg)
|
98
|
-
return ValidationError.new(msg, path, schema, error_symbol, data)
|
72
|
+
def schema_error(error_symbol, rule, path, val, args=nil)
|
73
|
+
return _create_error(SchemaError, error_symbol, rule, path, val, args)
|
99
74
|
end
|
100
75
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
76
|
+
def _create_error(error_klass, error_symbol, rule, path, val, args)
|
77
|
+
msg = Kwalify.msg(error_symbol)
|
78
|
+
assert_error("error_symbol=#{error_symbol.inspect}") unless msg
|
79
|
+
msg = msg % args if args
|
80
|
+
msg = "'#{val.to_s.gsub(/\n/, '\n')}': #{msg}" if val != nil && Kwalify.scalar_class?(val.class)
|
81
|
+
return error_klass.new(msg, path, val, rule, error_symbol)
|
105
82
|
end
|
106
83
|
|
107
|
-
|
108
|
-
module_function :assert_error, :validate_error, :schema_error, :error_message
|
109
|
-
|
110
84
|
end
|
111
85
|
|
86
|
+
extend ErrorHelper
|
87
|
+
|
112
88
|
end
|