dm-validations 0.9.11 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{History.txt → History.rdoc} +10 -3
- data/LICENSE +1 -0
- data/Manifest.txt +103 -33
- data/README.rdoc +172 -0
- data/Rakefile +4 -5
- data/lib/dm-validations.rb +73 -75
- data/lib/dm-validations/auto_validate.rb +98 -56
- data/lib/dm-validations/contextual_validators.rb +37 -14
- data/lib/dm-validations/exceptions.rb +3 -0
- data/lib/dm-validations/formats/url.rb +3 -1
- data/lib/dm-validations/support/context.rb +54 -0
- data/lib/dm-validations/validation_errors.rb +51 -27
- data/lib/dm-validations/{absent_field_validator.rb → validators/absent_field_validator.rb} +2 -8
- data/lib/dm-validations/{acceptance_validator.rb → validators/acceptance_validator.rb} +7 -7
- data/lib/dm-validations/{block_validator.rb → validators/block_validator.rb} +0 -0
- data/lib/dm-validations/{confirmation_validator.rb → validators/confirmation_validator.rb} +7 -7
- data/lib/dm-validations/{format_validator.rb → validators/format_validator.rb} +10 -16
- data/lib/dm-validations/{generic_validator.rb → validators/generic_validator.rb} +28 -6
- data/lib/dm-validations/validators/length_validator.rb +214 -0
- data/lib/dm-validations/validators/method_validator.rb +68 -0
- data/lib/dm-validations/validators/numeric_validator.rb +171 -0
- data/lib/dm-validations/{primitive_validator.rb → validators/primitive_validator.rb} +2 -7
- data/lib/dm-validations/{required_field_validator.rb → validators/required_field_validator.rb} +0 -6
- data/lib/dm-validations/{uniqueness_validator.rb → validators/uniqueness_validator.rb} +9 -20
- data/lib/dm-validations/{within_validator.rb → validators/within_validator.rb} +4 -4
- data/lib/dm-validations/version.rb +1 -1
- data/spec/fixtures/barcode.rb +32 -0
- data/spec/fixtures/basketball_court.rb +58 -0
- data/spec/fixtures/basketball_player.rb +37 -0
- data/spec/fixtures/beta_tester_account.rb +33 -0
- data/spec/fixtures/bill_of_landing.rb +42 -0
- data/spec/fixtures/boat_dock.rb +26 -0
- data/spec/fixtures/city.rb +25 -0
- data/spec/fixtures/company.rb +95 -0
- data/spec/fixtures/corporate_world.rb +47 -0
- data/spec/fixtures/country.rb +25 -0
- data/spec/fixtures/currency.rb +42 -0
- data/spec/fixtures/ethernet_frame.rb +56 -0
- data/spec/fixtures/event.rb +44 -0
- data/spec/fixtures/g3_concert.rb +57 -0
- data/spec/fixtures/jabberwock.rb +27 -0
- data/spec/fixtures/kayak.rb +28 -0
- data/spec/fixtures/lernean_hydra.rb +39 -0
- data/spec/fixtures/mathematical_function.rb +34 -0
- data/spec/fixtures/memory_object.rb +30 -0
- data/spec/fixtures/mittelschnauzer.rb +35 -0
- data/spec/fixtures/motor_launch.rb +21 -0
- data/spec/fixtures/page.rb +32 -0
- data/spec/fixtures/phone_number.rb +28 -0
- data/spec/fixtures/pirogue.rb +28 -0
- data/spec/fixtures/programming_language.rb +83 -0
- data/spec/fixtures/reservation.rb +38 -0
- data/spec/fixtures/scm_operation.rb +67 -0
- data/spec/fixtures/sms_message.rb +22 -0
- data/spec/fixtures/udp_packet.rb +49 -0
- data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +86 -0
- data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
- data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
- data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
- data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +47 -0
- data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +41 -0
- data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +104 -0
- data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +35 -0
- data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +33 -0
- data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +73 -0
- data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +121 -0
- data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +41 -0
- data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +20 -0
- data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +33 -0
- data/spec/integration/automatic_validation/spec_helper.rb +74 -0
- data/spec/integration/block_validator/block_validator_spec.rb +32 -0
- data/spec/integration/block_validator/spec_helper.rb +5 -0
- data/spec/integration/conditional_validation/if_condition_spec.rb +61 -0
- data/spec/integration/conditional_validation/spec_helper.rb +5 -0
- data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +74 -0
- data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
- data/spec/integration/datamapper_models/association_validation_spec.rb +23 -0
- data/spec/integration/datamapper_models/inheritance_spec.rb +78 -0
- data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +22 -0
- data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
- data/spec/integration/format_validator/email_format_validator_spec.rb +113 -0
- data/spec/integration/format_validator/format_validator_spec.rb +60 -0
- data/spec/integration/format_validator/regexp_validator_spec.rb +29 -0
- data/spec/integration/format_validator/spec_helper.rb +5 -0
- data/spec/integration/format_validator/url_format_validator_spec.rb +63 -0
- data/spec/integration/length_validator/default_value_spec.rb +12 -0
- data/spec/integration/length_validator/equality_spec.rb +79 -0
- data/spec/integration/length_validator/error_message_spec.rb +15 -18
- data/spec/integration/length_validator/maximum_spec.rb +40 -24
- data/spec/integration/length_validator/minimum_spec.rb +32 -21
- data/spec/integration/length_validator/range_spec.rb +50 -60
- data/spec/integration/length_validator/spec_helper.rb +7 -12
- data/spec/integration/method_validator/method_validator_spec.rb +239 -0
- data/spec/integration/method_validator/spec_helper.rb +5 -0
- data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +61 -0
- data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/float_type_spec.rb +42 -56
- data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/integer_only_true_spec.rb +3 -6
- data/spec/integration/numeric_validator/integer_type_spec.rb +8 -24
- data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +35 -0
- data/spec/integration/numeric_validator/spec_helper.rb +5 -77
- data/spec/integration/primitive_validator/primitive_validator_spec.rb +90 -0
- data/spec/integration/primitive_validator/spec_helper.rb +5 -0
- data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
- data/spec/integration/required_field_validator/association_spec.rb +5 -8
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +4 -7
- data/spec/integration/required_field_validator/date_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/datetime_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/float_type_value_spec.rb +3 -6
- data/spec/integration/required_field_validator/integer_type_value_spec.rb +2 -5
- data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +5 -8
- data/spec/integration/required_field_validator/shared_examples.rb +1 -1
- data/spec/integration/required_field_validator/spec_helper.rb +6 -67
- data/spec/integration/required_field_validator/string_type_value_spec.rb +2 -5
- data/spec/integration/required_field_validator/text_type_value_spec.rb +2 -5
- data/spec/integration/shared/default_validation_context.rb +13 -0
- data/spec/integration/shared/valid_and_invalid_model.rb +27 -0
- data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
- data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +114 -0
- data/spec/integration/within_validator/spec_helper.rb +5 -0
- data/spec/integration/within_validator/within_validator_spec.rb +164 -0
- data/spec/public/resource_spec.rb +68 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +12 -4
- data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
- data/spec/unit/contextual_validators/execution_spec.rb +55 -0
- data/spec/unit/contextual_validators/spec_helper.rb +37 -0
- data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
- data/spec/unit/validation_errors/adding_spec.rb +54 -0
- data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
- data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
- data/tasks/install.rb +1 -1
- data/tasks/spec.rb +4 -4
- metadata +118 -55
- data/README.txt +0 -72
- data/lib/dm-validations/custom_validator.rb +0 -72
- data/lib/dm-validations/length_validator.rb +0 -113
- data/lib/dm-validations/method_validator.rb +0 -68
- data/lib/dm-validations/numeric_validator.rb +0 -83
- data/spec/integration/absent_field_validator_spec.rb +0 -36
- data/spec/integration/acceptance_validator_spec.rb +0 -87
- data/spec/integration/auto_validate_spec.rb +0 -342
- data/spec/integration/block_validator_spec.rb +0 -30
- data/spec/integration/confirmation_validator_spec.rb +0 -105
- data/spec/integration/contextual_validators_spec.rb +0 -27
- data/spec/integration/custom_validator_spec.rb +0 -9
- data/spec/integration/format_validator_spec.rb +0 -193
- data/spec/integration/generic_validator_spec.rb +0 -17
- data/spec/integration/length_validator/valid_objects_spec.rb +0 -13
- data/spec/integration/method_validator_spec.rb +0 -58
- data/spec/integration/numeric_validator_spec.rb +0 -253
- data/spec/integration/primitive_validator_spec.rb +0 -30
- data/spec/integration/uniqueness_validator_spec.rb +0 -97
- data/spec/integration/validation_errors_spec.rb +0 -18
- data/spec/integration/validation_spec.rb +0 -404
- data/spec/integration/within_validator_spec.rb +0 -79
@@ -1,8 +1,15 @@
|
|
1
|
-
=== 0.
|
1
|
+
=== 0.10.0 / 2009-10-15
|
2
2
|
|
3
|
-
*
|
3
|
+
* 3 major enhancements:
|
4
|
+
|
5
|
+
* Major spec refactoring
|
6
|
+
* dm-validations now can be used with a pure Ruby classes, not just DM models
|
7
|
+
* i18n of error messages
|
8
|
+
|
9
|
+
* 1 minor enhancement:
|
10
|
+
|
11
|
+
* allow_nil option now uses #blank? instead of #nil? to comply common a use case
|
4
12
|
|
5
|
-
* Updated source file to force binary encoding
|
6
13
|
|
7
14
|
=== 0.9.10 / 2009-01-19
|
8
15
|
|
data/LICENSE
CHANGED
data/Manifest.txt
CHANGED
@@ -1,53 +1,113 @@
|
|
1
|
-
History.
|
1
|
+
History.rdoc
|
2
2
|
LICENSE
|
3
3
|
Manifest.txt
|
4
|
-
README.
|
4
|
+
README.rdoc
|
5
5
|
Rakefile
|
6
6
|
TODO
|
7
7
|
lib/dm-validations.rb
|
8
|
-
lib/dm-validations/absent_field_validator.rb
|
9
|
-
lib/dm-validations/acceptance_validator.rb
|
10
8
|
lib/dm-validations/auto_validate.rb
|
11
|
-
lib/dm-validations/block_validator.rb
|
12
|
-
lib/dm-validations/confirmation_validator.rb
|
13
9
|
lib/dm-validations/contextual_validators.rb
|
14
|
-
lib/dm-validations/
|
15
|
-
lib/dm-validations/format_validator.rb
|
10
|
+
lib/dm-validations/exceptions.rb
|
16
11
|
lib/dm-validations/formats/email.rb
|
17
12
|
lib/dm-validations/formats/url.rb
|
18
|
-
lib/dm-validations/
|
19
|
-
lib/dm-validations/length_validator.rb
|
20
|
-
lib/dm-validations/method_validator.rb
|
21
|
-
lib/dm-validations/numeric_validator.rb
|
22
|
-
lib/dm-validations/primitive_validator.rb
|
23
|
-
lib/dm-validations/required_field_validator.rb
|
13
|
+
lib/dm-validations/support/context.rb
|
24
14
|
lib/dm-validations/support/object.rb
|
25
|
-
lib/dm-validations/uniqueness_validator.rb
|
26
15
|
lib/dm-validations/validation_errors.rb
|
16
|
+
lib/dm-validations/validators/absent_field_validator.rb
|
17
|
+
lib/dm-validations/validators/acceptance_validator.rb
|
18
|
+
lib/dm-validations/validators/block_validator.rb
|
19
|
+
lib/dm-validations/validators/confirmation_validator.rb
|
20
|
+
lib/dm-validations/validators/format_validator.rb
|
21
|
+
lib/dm-validations/validators/generic_validator.rb
|
22
|
+
lib/dm-validations/validators/length_validator.rb
|
23
|
+
lib/dm-validations/validators/method_validator.rb
|
24
|
+
lib/dm-validations/validators/numeric_validator.rb
|
25
|
+
lib/dm-validations/validators/primitive_validator.rb
|
26
|
+
lib/dm-validations/validators/required_field_validator.rb
|
27
|
+
lib/dm-validations/validators/uniqueness_validator.rb
|
28
|
+
lib/dm-validations/validators/within_validator.rb
|
27
29
|
lib/dm-validations/version.rb
|
28
|
-
|
29
|
-
spec/
|
30
|
-
spec/
|
31
|
-
spec/
|
32
|
-
spec/
|
33
|
-
spec/
|
34
|
-
spec/
|
35
|
-
spec/
|
36
|
-
spec/
|
37
|
-
spec/
|
30
|
+
spec/fixtures/barcode.rb
|
31
|
+
spec/fixtures/basketball_court.rb
|
32
|
+
spec/fixtures/basketball_player.rb
|
33
|
+
spec/fixtures/beta_tester_account.rb
|
34
|
+
spec/fixtures/bill_of_landing.rb
|
35
|
+
spec/fixtures/boat_dock.rb
|
36
|
+
spec/fixtures/city.rb
|
37
|
+
spec/fixtures/company.rb
|
38
|
+
spec/fixtures/corporate_world.rb
|
39
|
+
spec/fixtures/country.rb
|
40
|
+
spec/fixtures/currency.rb
|
41
|
+
spec/fixtures/ethernet_frame.rb
|
42
|
+
spec/fixtures/event.rb
|
43
|
+
spec/fixtures/g3_concert.rb
|
44
|
+
spec/fixtures/jabberwock.rb
|
45
|
+
spec/fixtures/kayak.rb
|
46
|
+
spec/fixtures/lernean_hydra.rb
|
47
|
+
spec/fixtures/mathematical_function.rb
|
48
|
+
spec/fixtures/memory_object.rb
|
49
|
+
spec/fixtures/mittelschnauzer.rb
|
50
|
+
spec/fixtures/motor_launch.rb
|
51
|
+
spec/fixtures/page.rb
|
52
|
+
spec/fixtures/phone_number.rb
|
53
|
+
spec/fixtures/pirogue.rb
|
54
|
+
spec/fixtures/programming_language.rb
|
55
|
+
spec/fixtures/reservation.rb
|
56
|
+
spec/fixtures/scm_operation.rb
|
57
|
+
spec/fixtures/sms_message.rb
|
58
|
+
spec/fixtures/udp_packet.rb
|
59
|
+
spec/integration/absent_field_validator/absent_field_validator_spec.rb
|
60
|
+
spec/integration/absent_field_validator/spec_helper.rb
|
61
|
+
spec/integration/acceptance_validator/acceptance_validator_spec.rb
|
62
|
+
spec/integration/acceptance_validator/spec_helper.rb
|
63
|
+
spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
|
64
|
+
spec/integration/automatic_validation/disabling_inferred_validation_spec.rb
|
65
|
+
spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb
|
66
|
+
spec/integration/automatic_validation/inferred_float_property_validation_spec.rb
|
67
|
+
spec/integration/automatic_validation/inferred_format_validation_spec.rb
|
68
|
+
spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb
|
69
|
+
spec/integration/automatic_validation/inferred_length_validation_spec.rb
|
70
|
+
spec/integration/automatic_validation/inferred_presence_validation_spec.rb
|
71
|
+
spec/integration/automatic_validation/inferred_primitive_validation_spec.rb
|
72
|
+
spec/integration/automatic_validation/inferred_within_validation_spec.rb
|
73
|
+
spec/integration/automatic_validation/spec_helper.rb
|
74
|
+
spec/integration/block_validator/block_validator_spec.rb
|
75
|
+
spec/integration/block_validator/spec_helper.rb
|
76
|
+
spec/integration/conditional_validation/if_condition_spec.rb
|
77
|
+
spec/integration/conditional_validation/spec_helper.rb
|
78
|
+
spec/integration/confirmation_validator/confirmation_validator_spec.rb
|
79
|
+
spec/integration/confirmation_validator/spec_helper.rb
|
80
|
+
spec/integration/datamapper_models/association_validation_spec.rb
|
81
|
+
spec/integration/datamapper_models/inheritance_spec.rb
|
82
|
+
spec/integration/duplicated_validations/duplicated_validations_spec.rb
|
83
|
+
spec/integration/duplicated_validations/spec_helper.rb
|
84
|
+
spec/integration/format_validator/email_format_validator_spec.rb
|
85
|
+
spec/integration/format_validator/format_validator_spec.rb
|
86
|
+
spec/integration/format_validator/regexp_validator_spec.rb
|
87
|
+
spec/integration/format_validator/spec_helper.rb
|
88
|
+
spec/integration/format_validator/url_format_validator_spec.rb
|
89
|
+
spec/integration/length_validator/default_value_spec.rb
|
90
|
+
spec/integration/length_validator/equality_spec.rb
|
38
91
|
spec/integration/length_validator/error_message_spec.rb
|
39
92
|
spec/integration/length_validator/maximum_spec.rb
|
40
93
|
spec/integration/length_validator/minimum_spec.rb
|
41
94
|
spec/integration/length_validator/range_spec.rb
|
42
95
|
spec/integration/length_validator/spec_helper.rb
|
43
|
-
spec/integration/
|
44
|
-
spec/integration/
|
96
|
+
spec/integration/method_validator/method_validator_spec.rb
|
97
|
+
spec/integration/method_validator/spec_helper.rb
|
98
|
+
spec/integration/numeric_validator/equality_with_float_type_spec.rb
|
99
|
+
spec/integration/numeric_validator/equality_with_integer_type_spec.rb
|
45
100
|
spec/integration/numeric_validator/float_type_spec.rb
|
101
|
+
spec/integration/numeric_validator/gt_with_float_type_spec.rb
|
102
|
+
spec/integration/numeric_validator/gte_with_float_type_spec.rb
|
46
103
|
spec/integration/numeric_validator/integer_only_true_spec.rb
|
47
104
|
spec/integration/numeric_validator/integer_type_spec.rb
|
105
|
+
spec/integration/numeric_validator/lt_with_float_type_spec.rb
|
106
|
+
spec/integration/numeric_validator/lte_with_float_type_spec.rb
|
48
107
|
spec/integration/numeric_validator/spec_helper.rb
|
49
|
-
spec/integration/
|
50
|
-
spec/integration/
|
108
|
+
spec/integration/primitive_validator/primitive_validator_spec.rb
|
109
|
+
spec/integration/primitive_validator/spec_helper.rb
|
110
|
+
spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb
|
51
111
|
spec/integration/required_field_validator/association_spec.rb
|
52
112
|
spec/integration/required_field_validator/boolean_type_value_spec.rb
|
53
113
|
spec/integration/required_field_validator/date_type_value_spec.rb
|
@@ -59,11 +119,21 @@ spec/integration/required_field_validator/shared_examples.rb
|
|
59
119
|
spec/integration/required_field_validator/spec_helper.rb
|
60
120
|
spec/integration/required_field_validator/string_type_value_spec.rb
|
61
121
|
spec/integration/required_field_validator/text_type_value_spec.rb
|
62
|
-
spec/integration/
|
63
|
-
spec/integration/
|
64
|
-
spec/integration/
|
65
|
-
spec/integration/
|
122
|
+
spec/integration/shared/default_validation_context.rb
|
123
|
+
spec/integration/shared/valid_and_invalid_model.rb
|
124
|
+
spec/integration/uniqueness_validator/spec_helper.rb
|
125
|
+
spec/integration/uniqueness_validator/uniqueness_validator_spec.rb
|
126
|
+
spec/integration/within_validator/spec_helper.rb
|
127
|
+
spec/integration/within_validator/within_validator_spec.rb
|
128
|
+
spec/public/resource_spec.rb
|
66
129
|
spec/spec.opts
|
67
130
|
spec/spec_helper.rb
|
131
|
+
spec/unit/contextual_validators/emptiness_spec.rb
|
132
|
+
spec/unit/contextual_validators/execution_spec.rb
|
133
|
+
spec/unit/contextual_validators/spec_helper.rb
|
134
|
+
spec/unit/generic_validator/equality_operator_spec.rb
|
135
|
+
spec/unit/validation_errors/adding_spec.rb
|
136
|
+
spec/unit/validation_errors/emptiness_spec.rb
|
137
|
+
spec/unit/validation_errors/enumerable_spec.rb
|
68
138
|
tasks/install.rb
|
69
139
|
tasks/spec.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
This is a DataMapper plugin that provides validations for DataMapper model
|
2
|
+
classes.
|
3
|
+
|
4
|
+
== Setup
|
5
|
+
DataMapper validation capabilities are automatically available for DataMapper
|
6
|
+
resources when you require dm-validations' into your application. For
|
7
|
+
pure Ruby classes, require 'dm-validations' and then include DataMapper::Validate
|
8
|
+
module like so:
|
9
|
+
|
10
|
+
class ProgrammingLanguage
|
11
|
+
#
|
12
|
+
# Behaviors
|
13
|
+
#
|
14
|
+
|
15
|
+
include ::DataMapper::Validate
|
16
|
+
|
17
|
+
#
|
18
|
+
# Accessors
|
19
|
+
#
|
20
|
+
|
21
|
+
attr_accessor :name, :allows_manual_memory_management, :allows_optional_parentheses
|
22
|
+
|
23
|
+
#
|
24
|
+
# Validations
|
25
|
+
#
|
26
|
+
|
27
|
+
validates_present :name
|
28
|
+
validates_with_method :ensure_allows_optional_parentheses, :when => [:implementing_a_dsl]
|
29
|
+
validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
== Specifying Model Validations
|
34
|
+
There are two primary ways to implement validations for your models:
|
35
|
+
|
36
|
+
1) Placing validation methods with properties as params in your class
|
37
|
+
definitions like:
|
38
|
+
- validates_length :name
|
39
|
+
- validates_length [:name, :description]
|
40
|
+
|
41
|
+
2) Using auto-validations, please see DataMapper::Validate::AutoValidate
|
42
|
+
|
43
|
+
An example class with validations declared:
|
44
|
+
|
45
|
+
require 'dm-validations'
|
46
|
+
|
47
|
+
class Account
|
48
|
+
include DataMapper::Resource
|
49
|
+
|
50
|
+
property :name, String
|
51
|
+
validates_length :name
|
52
|
+
end
|
53
|
+
|
54
|
+
See all of the DataMapper::Validate module's XYZValidator(s) to learn about the
|
55
|
+
complete collections of validators available to you.
|
56
|
+
|
57
|
+
== Validating
|
58
|
+
DataMapper validations, when included, alter the default save/create/update
|
59
|
+
process for a model. Unless you specify a context the resource must be
|
60
|
+
valid in the :default context before saving.
|
61
|
+
|
62
|
+
You may manually validate a resource using the valid? method, which will
|
63
|
+
return true if the resource is valid, and false if it is invalid.
|
64
|
+
|
65
|
+
In addition to the valid? method, there is also an all_valid? method that
|
66
|
+
recursively walks both the current object and its associated objects and returns
|
67
|
+
its true/false result for the entire walk.
|
68
|
+
|
69
|
+
== Working with Validation Errors
|
70
|
+
If your validators find errors in your model, they will populate the
|
71
|
+
DataMapper::Validate::ValidationErrors object that is available through each of
|
72
|
+
your models via calls to your model's errors method.
|
73
|
+
|
74
|
+
For example:
|
75
|
+
|
76
|
+
my_account = Account.new(:name => "Jose")
|
77
|
+
if my_account.save
|
78
|
+
# my_account is valid and has been saved
|
79
|
+
else
|
80
|
+
my_account.errors.each do |e|
|
81
|
+
puts e
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
See DataMapper::Validate::ValidationErrors for all you can do with your model's
|
86
|
+
errors method.
|
87
|
+
|
88
|
+
== Contextual Validations
|
89
|
+
|
90
|
+
DataMapper Validations also provide a means of grouping your validations into
|
91
|
+
contexts. This enables you to run different sets of validations when you
|
92
|
+
need it. For instance, the same model may not only behave differently
|
93
|
+
when initially saved or saved on update, but also require special validation sets
|
94
|
+
for publishing, exporting, importing and so on.
|
95
|
+
|
96
|
+
Again, using our example for pure Ruby class validations:
|
97
|
+
|
98
|
+
class ProgrammingLanguage
|
99
|
+
#
|
100
|
+
# Behaviors
|
101
|
+
#
|
102
|
+
|
103
|
+
include ::DataMapper::Validate
|
104
|
+
|
105
|
+
#
|
106
|
+
# Accessors
|
107
|
+
#
|
108
|
+
|
109
|
+
attr_accessor :name, :allows_manual_memory_management, :allows_optional_parentheses
|
110
|
+
|
111
|
+
#
|
112
|
+
# Validations
|
113
|
+
#
|
114
|
+
|
115
|
+
validates_present :name
|
116
|
+
validates_with_method :ensure_allows_optional_parentheses, :when => [:implementing_a_dsl]
|
117
|
+
validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
|
118
|
+
end
|
119
|
+
|
120
|
+
ProgrammingLanguage instance now use #valid? method with one of two context symbols:
|
121
|
+
|
122
|
+
@ruby.valid?(:implementing_a_dsl) # => true
|
123
|
+
@ruby.valid?(:doing_system_programming) # => false
|
124
|
+
|
125
|
+
@c.valid?(:implementing_a_dsl) # => false
|
126
|
+
@c.valid?(:doing_system_programming) # => true
|
127
|
+
|
128
|
+
Each context causes different set of validations to be triggered. If you don't
|
129
|
+
specify a context using :when, :on or :group options (they are all aliases and do
|
130
|
+
the same thing), default context name is :default. When you do model.valid? (without
|
131
|
+
specifying context explicitly), again, :default context is used. One validation
|
132
|
+
can be used in two, three or five contexts if you like:
|
133
|
+
|
134
|
+
class Book
|
135
|
+
|
136
|
+
#
|
137
|
+
# Behaviors
|
138
|
+
#
|
139
|
+
|
140
|
+
# this time it is a DM model
|
141
|
+
include ::DataMapper::Resource
|
142
|
+
|
143
|
+
#
|
144
|
+
# Accessors
|
145
|
+
#
|
146
|
+
|
147
|
+
property :id, Serial
|
148
|
+
property :name, String
|
149
|
+
|
150
|
+
property :agreed_title, String
|
151
|
+
property :finished_toc, Boolean
|
152
|
+
|
153
|
+
#
|
154
|
+
# Validations
|
155
|
+
#
|
156
|
+
|
157
|
+
# used in all contexts, including default
|
158
|
+
validates_present :name, :when => [:default, :sending_to_print]
|
159
|
+
validates_present :agreed_title, :when => [:sending_to_print]
|
160
|
+
|
161
|
+
validates_with_block :toc, :when => [:sending_to_print] do
|
162
|
+
if self.finished_toc
|
163
|
+
[true]
|
164
|
+
else
|
165
|
+
[false, "TOC must be finalized before you send a book to print"]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
In the example above, name is validated for presence in both :default context and
|
171
|
+
:sending_to_print context, while TOC related block validation and title presence validation
|
172
|
+
only take place in :sending_to_print context.
|
data/Rakefile
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'pathname'
|
2
|
-
require 'rubygems'
|
3
2
|
|
4
3
|
ROOT = Pathname(__FILE__).dirname.expand_path
|
5
4
|
JRUBY = RUBY_PLATFORM =~ /java/
|
@@ -8,17 +7,17 @@ SUDO = (WINDOWS || JRUBY) ? '' : ('sudo' unless ENV['SUDOLESS'])
|
|
8
7
|
|
9
8
|
require ROOT + 'lib/dm-validations/version'
|
10
9
|
|
11
|
-
AUTHOR = 'Guy van den Berg'
|
10
|
+
AUTHOR = 'Guy van den Berg, DataMapper development team'
|
12
11
|
EMAIL = 'vandenberg.guy [a] gmail [d] com'
|
13
12
|
GEM_NAME = 'dm-validations'
|
14
13
|
GEM_VERSION = DataMapper::Validations::VERSION
|
15
14
|
GEM_DEPENDENCIES = [['dm-core', GEM_VERSION]]
|
16
15
|
GEM_CLEAN = %w[ log pkg coverage ]
|
17
|
-
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.
|
16
|
+
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.rdoc LICENSE TODO History.rdoc ] }
|
18
17
|
|
19
18
|
PROJECT_NAME = 'datamapper'
|
20
|
-
PROJECT_URL = "http://github.com/
|
21
|
-
PROJECT_DESCRIPTION = PROJECT_SUMMARY = '
|
19
|
+
PROJECT_URL = "http://github.com/datamapper/dm-more/tree/master/#{GEM_NAME}"
|
20
|
+
PROJECT_DESCRIPTION = PROJECT_SUMMARY = 'Library for performing validations on DM models and pure Ruby object'
|
22
21
|
|
23
22
|
[ ROOT, ROOT.parent ].each do |dir|
|
24
23
|
Pathname.glob(dir.join('tasks/**/*.rb').to_s).each { |f| require f }
|
data/lib/dm-validations.rb
CHANGED
@@ -1,75 +1,71 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require dir / 'method_validator'
|
23
|
-
require dir / 'block_validator'
|
24
|
-
require dir / 'uniqueness_validator'
|
25
|
-
require dir / 'acceptance_validator'
|
26
|
-
require dir / 'custom_validator'
|
27
|
-
|
28
|
-
require dir / 'support' / 'object'
|
1
|
+
require 'dm-validations/exceptions'
|
2
|
+
require 'dm-validations/validation_errors'
|
3
|
+
require 'dm-validations/contextual_validators'
|
4
|
+
require 'dm-validations/auto_validate'
|
5
|
+
|
6
|
+
require 'dm-validations/validators/generic_validator'
|
7
|
+
require 'dm-validations/validators/required_field_validator'
|
8
|
+
require 'dm-validations/validators/primitive_validator'
|
9
|
+
require 'dm-validations/validators/absent_field_validator'
|
10
|
+
require 'dm-validations/validators/confirmation_validator'
|
11
|
+
require 'dm-validations/validators/format_validator'
|
12
|
+
require 'dm-validations/validators/length_validator'
|
13
|
+
require 'dm-validations/validators/within_validator'
|
14
|
+
require 'dm-validations/validators/numeric_validator'
|
15
|
+
require 'dm-validations/validators/method_validator'
|
16
|
+
require 'dm-validations/validators/block_validator'
|
17
|
+
require 'dm-validations/validators/uniqueness_validator'
|
18
|
+
require 'dm-validations/validators/acceptance_validator'
|
19
|
+
|
20
|
+
require 'dm-validations/support/context'
|
21
|
+
require 'dm-validations/support/object'
|
29
22
|
|
30
23
|
module DataMapper
|
31
24
|
module Validate
|
25
|
+
Model.append_inclusions self
|
26
|
+
|
27
|
+
extend Chainable
|
32
28
|
|
33
29
|
def self.included(model)
|
34
|
-
model.class_eval <<-
|
35
|
-
|
36
|
-
|
30
|
+
model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
31
|
+
def self.create(attributes = {}, *args)
|
32
|
+
resource = new(attributes)
|
33
|
+
resource.save(*args)
|
34
|
+
resource
|
37
35
|
end
|
36
|
+
RUBY
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
return resource unless resource.valid?(context)
|
43
|
-
resource.save!
|
44
|
-
resource
|
45
|
-
end
|
46
|
-
|
47
|
-
def create!(attributes = {})
|
48
|
-
resource = new(attributes)
|
49
|
-
resource.save!
|
50
|
-
resource
|
51
|
-
end
|
52
|
-
end
|
53
|
-
EOS
|
38
|
+
# models that are non DM resources must get .validators
|
39
|
+
# and other methods, too
|
40
|
+
model.extend ClassMethods
|
54
41
|
end
|
55
42
|
|
56
43
|
# Ensures the object is valid for the context provided, and otherwise
|
57
44
|
# throws :halt and returns false.
|
58
45
|
#
|
59
|
-
|
60
|
-
|
46
|
+
chainable do
|
47
|
+
def save(context = default_validation_context)
|
48
|
+
validation_context(context) { super() }
|
49
|
+
end
|
61
50
|
end
|
62
51
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
52
|
+
chainable do
|
53
|
+
def save_self(*)
|
54
|
+
return false unless validation_context_stack.empty? || valid?(current_validation_context)
|
55
|
+
super
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
chainable do
|
60
|
+
def update(attributes = {}, context = default_validation_context)
|
61
|
+
validation_context(context) { super(attributes) }
|
62
|
+
end
|
67
63
|
end
|
68
64
|
|
69
65
|
# Return the ValidationErrors
|
70
66
|
#
|
71
67
|
def errors
|
72
|
-
@errors ||= ValidationErrors.new
|
68
|
+
@errors ||= ValidationErrors.new(self)
|
73
69
|
end
|
74
70
|
|
75
71
|
# Mark this resource as validatable. When we validate associations of a
|
@@ -89,7 +85,8 @@ module DataMapper
|
|
89
85
|
# Check if a resource is valid in a given context
|
90
86
|
#
|
91
87
|
def valid?(context = :default)
|
92
|
-
self.class
|
88
|
+
klass = respond_to?(:model) ? model : self.class
|
89
|
+
klass.validators.execute(context, self)
|
93
90
|
end
|
94
91
|
|
95
92
|
# Begin a recursive walk of the model checking validity
|
@@ -117,9 +114,8 @@ module DataMapper
|
|
117
114
|
return valid && target.valid?
|
118
115
|
end
|
119
116
|
|
120
|
-
|
121
117
|
def validation_property_value(name)
|
122
|
-
|
118
|
+
respond_to?(name, true) ? send(name) : nil
|
123
119
|
end
|
124
120
|
|
125
121
|
# Get the corresponding Resource property, if it exists.
|
@@ -127,7 +123,7 @@ module DataMapper
|
|
127
123
|
# Note: DataMapper validations can be used on non-DataMapper resources.
|
128
124
|
# In such cases, the return value will be nil.
|
129
125
|
def validation_property(field_name)
|
130
|
-
if respond_to?(:model) && (properties = model.properties(
|
126
|
+
if respond_to?(:model) && (properties = model.properties(repository.name)) && properties.named?(field_name)
|
131
127
|
properties[field_name]
|
132
128
|
end
|
133
129
|
end
|
@@ -171,13 +167,9 @@ module DataMapper
|
|
171
167
|
#
|
172
168
|
def opts_from_validator_args(args, defaults = nil)
|
173
169
|
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
174
|
-
context = :default
|
175
|
-
context = opts[:context] if opts.has_key?(:context)
|
176
|
-
context = opts.delete(:on) if opts.has_key?(:on)
|
177
|
-
context = opts.delete(:when) if opts.has_key?(:when)
|
178
|
-
context = opts.delete(:group) if opts.has_key?(:group)
|
170
|
+
context = opts.delete(:group) || opts.delete(:on) || opts.delete(:when) || opts.delete(:context) || :default
|
179
171
|
opts[:context] = context
|
180
|
-
opts.
|
172
|
+
opts.merge!(defaults) unless defaults.nil?
|
181
173
|
opts
|
182
174
|
end
|
183
175
|
|
@@ -187,30 +179,39 @@ module DataMapper
|
|
187
179
|
#
|
188
180
|
def create_context_instance_methods(context)
|
189
181
|
name = "valid_for_#{context.to_s}?" # valid_for_signup?
|
190
|
-
if !
|
191
|
-
class_eval <<-
|
182
|
+
if !instance_methods.include?(name)
|
183
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
192
184
|
def #{name} # def valid_for_signup?
|
193
|
-
valid?(
|
185
|
+
valid?(#{context.to_sym.inspect}) # valid?(:signup)
|
194
186
|
end # end
|
195
|
-
|
187
|
+
RUBY
|
196
188
|
end
|
197
189
|
|
198
190
|
all = "all_valid_for_#{context.to_s}?" # all_valid_for_signup?
|
199
|
-
if !
|
200
|
-
class_eval <<-
|
191
|
+
if !instance_methods.include?(all)
|
192
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
201
193
|
def #{all} # def all_valid_for_signup?
|
202
|
-
all_valid?(
|
194
|
+
all_valid?(#{context.to_sym.inspect}) # all_valid?(:signup)
|
203
195
|
end # end
|
204
|
-
|
196
|
+
RUBY
|
205
197
|
end
|
206
198
|
end
|
207
199
|
|
208
200
|
# Create a new validator of the given klazz and push it onto the
|
209
201
|
# requested context for each of the attributes in the fields list
|
202
|
+
# @param [Hash] opts
|
203
|
+
# Options supplied to validation macro, example:
|
204
|
+
# {:context=>:default, :maximum=>50, :allow_nil=>true, :message=>nil}
|
205
|
+
#
|
206
|
+
# @param [Array<Symbol>] fields
|
207
|
+
# Fields given to validation macro, example:
|
208
|
+
# [:first_name, :last_name] in validates_present :first_name, :last_name
|
210
209
|
#
|
210
|
+
# @param [Class] klazz
|
211
|
+
# Validator class, example: DataMapper::Validate::LengthValidator
|
211
212
|
def add_validator_to_context(opts, fields, klazz)
|
212
213
|
fields.each do |field|
|
213
|
-
validator = klazz.new(field, opts)
|
214
|
+
validator = klazz.new(field, opts.dup)
|
214
215
|
if opts[:context].is_a?(Symbol)
|
215
216
|
unless validators.context(opts[:context]).include?(validator)
|
216
217
|
validators.context(opts[:context]) << validator
|
@@ -228,7 +229,4 @@ module DataMapper
|
|
228
229
|
end
|
229
230
|
end # module ClassMethods
|
230
231
|
end # module Validate
|
231
|
-
|
232
|
-
Resource.append_inclusions Validate
|
233
|
-
Model.append_extensions Validate::ClassMethods
|
234
232
|
end # module DataMapper
|