domainic-type 0.1.0.alpha.2.1.0 → 0.1.0.alpha.3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/LICENSE +1 -1
- data/README.md +28 -4
- data/lib/domainic/type/accessors.rb +41 -0
- data/lib/domainic/type/behavior/enumerable_behavior.rb +262 -0
- data/lib/domainic/type/behavior/numeric_behavior.rb +340 -0
- data/lib/domainic/type/behavior/sizable_behavior.rb +246 -0
- data/lib/domainic/type/behavior/string_behavior.rb +379 -0
- data/lib/domainic/type/behavior.rb +239 -0
- data/lib/domainic/type/config/registry.yml +101 -0
- data/lib/domainic/type/constraint/behavior.rb +342 -0
- data/lib/domainic/type/constraint/constraints/all_constraint.rb +81 -0
- data/lib/domainic/type/constraint/constraints/and_constraint.rb +105 -0
- data/lib/domainic/type/constraint/constraints/any_constraint.rb +83 -0
- data/lib/domainic/type/constraint/constraints/case_constraint.rb +104 -0
- data/lib/domainic/type/constraint/constraints/character_set_constraint.rb +111 -0
- data/lib/domainic/type/constraint/constraints/divisibility_constraint.rb +126 -0
- data/lib/domainic/type/constraint/constraints/emptiness_constraint.rb +69 -0
- data/lib/domainic/type/constraint/constraints/equality_constraint.rb +75 -0
- data/lib/domainic/type/constraint/constraints/finiteness_constraint.rb +123 -0
- data/lib/domainic/type/constraint/constraints/inclusion_constraint.rb +74 -0
- data/lib/domainic/type/constraint/constraints/match_pattern_constraint.rb +87 -0
- data/lib/domainic/type/constraint/constraints/method_presence_constraint.rb +72 -0
- data/lib/domainic/type/constraint/constraints/none_constraint.rb +83 -0
- data/lib/domainic/type/constraint/constraints/nor_constraint.rb +105 -0
- data/lib/domainic/type/constraint/constraints/not_constraint.rb +76 -0
- data/lib/domainic/type/constraint/constraints/or_constraint.rb +106 -0
- data/lib/domainic/type/constraint/constraints/ordering_constraint.rb +75 -0
- data/lib/domainic/type/constraint/constraints/parity_constraint.rb +102 -0
- data/lib/domainic/type/constraint/constraints/polarity_constraint.rb +147 -0
- data/lib/domainic/type/constraint/constraints/range_constraint.rb +135 -0
- data/lib/domainic/type/constraint/constraints/type_constraint.rb +110 -0
- data/lib/domainic/type/constraint/constraints/uniqueness_constraint.rb +69 -0
- data/lib/domainic/type/constraint/resolver.rb +172 -0
- data/lib/domainic/type/constraint/set.rb +266 -0
- data/lib/domainic/type/definitions.rb +364 -0
- data/lib/domainic/type/types/core/array_type.rb +48 -0
- data/lib/domainic/type/types/core/float_type.rb +39 -0
- data/lib/domainic/type/types/core/hash_type.rb +143 -0
- data/lib/domainic/type/types/core/integer_type.rb +38 -0
- data/lib/domainic/type/types/core/string_type.rb +51 -0
- data/lib/domainic/type/types/core/symbol_type.rb +51 -0
- data/lib/domainic/type/types/specification/anything_type.rb +22 -0
- data/lib/domainic/type/types/specification/duck_type.rb +55 -0
- data/lib/domainic/type/types/specification/enum_type.rb +26 -0
- data/lib/domainic/type/types/specification/union_type.rb +26 -0
- data/lib/domainic/type/types/specification/void_type.rb +12 -0
- data/lib/domainic/type.rb +7 -0
- data/lib/domainic-type.rb +3 -0
- data/sig/domainic/type/accessors.rbs +22 -0
- data/sig/domainic/type/behavior/enumerable_behavior.rbs +238 -0
- data/sig/domainic/type/behavior/numeric_behavior.rbs +299 -0
- data/sig/domainic/type/behavior/sizable_behavior.rbs +218 -0
- data/sig/domainic/type/behavior/string_behavior.rbs +315 -0
- data/sig/domainic/type/behavior.rbs +153 -0
- data/sig/domainic/type/constraint/behavior.rbs +258 -0
- data/sig/domainic/type/constraint/constraints/all_constraint.rbs +55 -0
- data/sig/domainic/type/constraint/constraints/and_constraint.rbs +72 -0
- data/sig/domainic/type/constraint/constraints/any_constraint.rbs +57 -0
- data/sig/domainic/type/constraint/constraints/case_constraint.rbs +73 -0
- data/sig/domainic/type/constraint/constraints/character_set_constraint.rbs +82 -0
- data/sig/domainic/type/constraint/constraints/divisibility_constraint.rbs +91 -0
- data/sig/domainic/type/constraint/constraints/emptiness_constraint.rbs +54 -0
- data/sig/domainic/type/constraint/constraints/equality_constraint.rbs +60 -0
- data/sig/domainic/type/constraint/constraints/finiteness_constraint.rbs +82 -0
- data/sig/domainic/type/constraint/constraints/inclusion_constraint.rbs +59 -0
- data/sig/domainic/type/constraint/constraints/match_pattern_constraint.rbs +66 -0
- data/sig/domainic/type/constraint/constraints/method_presence_constraint.rbs +51 -0
- data/sig/domainic/type/constraint/constraints/none_constraint.rbs +57 -0
- data/sig/domainic/type/constraint/constraints/nor_constraint.rbs +72 -0
- data/sig/domainic/type/constraint/constraints/not_constraint.rbs +56 -0
- data/sig/domainic/type/constraint/constraints/or_constraint.rbs +74 -0
- data/sig/domainic/type/constraint/constraints/ordering_constraint.rbs +60 -0
- data/sig/domainic/type/constraint/constraints/parity_constraint.rbs +71 -0
- data/sig/domainic/type/constraint/constraints/polarity_constraint.rbs +101 -0
- data/sig/domainic/type/constraint/constraints/range_constraint.rbs +88 -0
- data/sig/domainic/type/constraint/constraints/type_constraint.rbs +86 -0
- data/sig/domainic/type/constraint/constraints/uniqueness_constraint.rbs +54 -0
- data/sig/domainic/type/constraint/resolver.rbs +117 -0
- data/sig/domainic/type/constraint/set.rbs +159 -0
- data/sig/domainic/type/definitions.rbs +304 -0
- data/sig/domainic/type/types/core/array_type.rbs +42 -0
- data/sig/domainic/type/types/core/float_type.rbs +33 -0
- data/sig/domainic/type/types/core/hash_type.rbs +107 -0
- data/sig/domainic/type/types/core/integer_type.rbs +32 -0
- data/sig/domainic/type/types/core/string_type.rbs +45 -0
- data/sig/domainic/type/types/core/symbol_type.rbs +45 -0
- data/sig/domainic/type/types/specification/anything_type.rbs +14 -0
- data/sig/domainic/type/types/specification/duck_type.rbs +41 -0
- data/sig/domainic/type/types/specification/enum_type.rbs +14 -0
- data/sig/domainic/type/types/specification/union_type.rbs +14 -0
- data/sig/domainic/type/types/specification/void_type.rbs +8 -0
- data/sig/domainic/type.rbs +5 -0
- data/sig/domainic-type.rbs +1 -0
- data/sig/manifest.yaml +2 -0
- metadata +108 -71
@@ -0,0 +1,54 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint for validating that a collection is empty.
|
5
|
+
#
|
6
|
+
# This constraint ensures that an enumerable collection contains no elements
|
7
|
+
# by checking the collection's #empty? method. It works with any object that
|
8
|
+
# includes the Enumerable module and responds to #empty?.
|
9
|
+
#
|
10
|
+
# @example Basic emptiness validation
|
11
|
+
# constraint = EmptinessConstraint.new(:self)
|
12
|
+
# constraint.satisfied?([]) # => true
|
13
|
+
# constraint.satisfied?([1, 2, 3]) # => false
|
14
|
+
#
|
15
|
+
# @example With different collection types
|
16
|
+
# constraint = EmptinessConstraint.new(:self)
|
17
|
+
# constraint.satisfied?(Set.new) # => true
|
18
|
+
# constraint.satisfied?(['a', 'b']) # => false
|
19
|
+
#
|
20
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
21
|
+
# @since 0.1.0
|
22
|
+
class EmptinessConstraint
|
23
|
+
include Behavior[nil, untyped, { }]
|
24
|
+
|
25
|
+
# Get a human-readable description of the emptiness requirement.
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# constraint = EmptinessConstraint.new(:self)
|
29
|
+
# constraint.description # => "empty"
|
30
|
+
#
|
31
|
+
# @return [String] A description of the emptiness requirement
|
32
|
+
def short_description: ...
|
33
|
+
|
34
|
+
# Get a human-readable description of why emptiness validation failed.
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# constraint = EmptinessConstraint.new(:self)
|
38
|
+
# constraint.satisfied?([1, 2, 3])
|
39
|
+
# constraint.short_violation_description # => "not empty"
|
40
|
+
#
|
41
|
+
# @return [String] A description of the emptiness failure
|
42
|
+
def short_violation_description: ...
|
43
|
+
|
44
|
+
# Check if the collection is empty.
|
45
|
+
#
|
46
|
+
# Uses the collection's #empty? method to determine if it contains
|
47
|
+
# any elements.
|
48
|
+
#
|
49
|
+
# @return [Boolean] true if the collection is empty
|
50
|
+
def satisfies_constraint?: ...
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint for validating that values are equal to an expected value.
|
5
|
+
#
|
6
|
+
# This constraint uses Ruby's standard equality operator (==) to compare values,
|
7
|
+
# allowing for type-specific equality definitions while maintaining consistent
|
8
|
+
# behavior across different value types.
|
9
|
+
#
|
10
|
+
# @example Basic equality validation
|
11
|
+
# constraint = EqualityConstraint.new(:self, 42)
|
12
|
+
# constraint.satisfied?(42) # => true
|
13
|
+
# constraint.satisfied?(41) # => false
|
14
|
+
#
|
15
|
+
# @example Complex object comparison
|
16
|
+
# point = Struct.new(:x, :y).new(1, 2)
|
17
|
+
# constraint = EqualityConstraint.new(:self, point)
|
18
|
+
# constraint.satisfied?(Struct.new(:x, :y).new(1, 2)) # => true
|
19
|
+
# constraint.satisfied?(Struct.new(:x, :y).new(2, 1)) # => false
|
20
|
+
#
|
21
|
+
# @example Array comparison
|
22
|
+
# constraint = EqualityConstraint.new(:self, [1, 2, 3])
|
23
|
+
# constraint.satisfied?([1, 2, 3]) # => true
|
24
|
+
# constraint.satisfied?([3, 2, 1]) # => false
|
25
|
+
#
|
26
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
27
|
+
# @since 0.1.0
|
28
|
+
class EqualityConstraint
|
29
|
+
include Behavior[untyped, untyped, { }]
|
30
|
+
|
31
|
+
# Get a human-readable description of the equality requirement.
|
32
|
+
#
|
33
|
+
# @example
|
34
|
+
# constraint = EqualityConstraint.new(:self, 42)
|
35
|
+
# constraint.description # => "equal to 42"
|
36
|
+
#
|
37
|
+
# @return [String] A description of the expected value
|
38
|
+
def short_description: ...
|
39
|
+
|
40
|
+
# Get a human-readable description of why equality validation failed.
|
41
|
+
#
|
42
|
+
# @example
|
43
|
+
# constraint = EqualityConstraint.new(:self, 42)
|
44
|
+
# constraint.satisfied?(41)
|
45
|
+
# constraint.short_violation_description # => "not equal to 42"
|
46
|
+
#
|
47
|
+
# @return [String] A description of the equality failure
|
48
|
+
def short_violation_description: ...
|
49
|
+
|
50
|
+
# Check if the actual value equals the expected value.
|
51
|
+
#
|
52
|
+
# Uses Ruby's standard equality operator (==) for comparison, allowing
|
53
|
+
# objects to define their own equality behavior.
|
54
|
+
#
|
55
|
+
# @return [Boolean] true if the values are equal
|
56
|
+
def satisfies_constraint?: ...
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint for validating that numeric values are finite or infinite.
|
5
|
+
#
|
6
|
+
# This constraint verifies whether a numeric value is finite or infinite by calling
|
7
|
+
# Ruby's standard finite?/infinite? methods. It's useful for ensuring numbers remain
|
8
|
+
# within computable bounds and handling edge cases in numerical computations.
|
9
|
+
#
|
10
|
+
# @example Basic finiteness check
|
11
|
+
# constraint = FinitenessConstraint.new(:self).expecting(:finite)
|
12
|
+
# constraint.satisfied?(42) # => true
|
13
|
+
# constraint.satisfied?(Float::INFINITY) # => false
|
14
|
+
#
|
15
|
+
# @example Checking for infinity
|
16
|
+
# constraint = FinitenessConstraint.new(:self).expecting(:infinite)
|
17
|
+
# constraint.satisfied?(Float::INFINITY) # => true
|
18
|
+
# constraint.satisfied?(42) # => false
|
19
|
+
#
|
20
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
21
|
+
# @since 0.1.0
|
22
|
+
class FinitenessConstraint
|
23
|
+
type expected = :finite | :finite? | :infinite | :infinite?
|
24
|
+
|
25
|
+
include Behavior[expected, Numeric, { }]
|
26
|
+
|
27
|
+
# Get a human-readable description of the finiteness requirement.
|
28
|
+
#
|
29
|
+
# @return [String] Description of the finiteness requirement
|
30
|
+
def short_description: ...
|
31
|
+
|
32
|
+
# Get a human-readable description of why finiteness validation failed.
|
33
|
+
#
|
34
|
+
# @return [String] Description of the validation failure
|
35
|
+
def short_violation_description: ...
|
36
|
+
|
37
|
+
# Coerce the expectation into the correct method name format.
|
38
|
+
#
|
39
|
+
# Ensures the expectation ends with a question mark to match Ruby's
|
40
|
+
# standard method naming for predicate methods.
|
41
|
+
#
|
42
|
+
# @param expectation [Symbol, String] The finiteness check to perform
|
43
|
+
#
|
44
|
+
# @return [Symbol] The coerced method name
|
45
|
+
def coerce_expectation: ...
|
46
|
+
|
47
|
+
# Check if the value satisfies the finiteness constraint.
|
48
|
+
#
|
49
|
+
# @return [Boolean] true if the value matches the finiteness requirement
|
50
|
+
def satisfies_constraint?: ...
|
51
|
+
|
52
|
+
# Validate that the expectation is a valid finiteness check.
|
53
|
+
#
|
54
|
+
# @param expectation [Object] The value to validate
|
55
|
+
#
|
56
|
+
# @raise [ArgumentError] if the expectation is not :finite, :finite?, :infinite, or :infinite?
|
57
|
+
# @return [void]
|
58
|
+
def validate_expectation!: ...
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Interpret the result from Ruby's finite?/infinite? methods.
|
63
|
+
#
|
64
|
+
# Handles the different return values from Ruby's finiteness checking methods:
|
65
|
+
# - finite? returns true/false
|
66
|
+
# - infinite? returns nil (for finite numbers), 1 (for +∞), or -1 (for -∞)
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# interpret_result(true) # => true
|
70
|
+
# interpret_result(false) # => false
|
71
|
+
# interpret_result(nil) # => false
|
72
|
+
# interpret_result(1) # => true
|
73
|
+
# interpret_result(-1) # => true
|
74
|
+
#
|
75
|
+
# @param result [Boolean, Integer, nil] The result from finite? or infinite?
|
76
|
+
#
|
77
|
+
# @return [Boolean] true if the result indicates the desired finiteness state
|
78
|
+
def interpret_result: ((bool | Numeric)? result) -> bool
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint for validating that a collection includes a specific value.
|
5
|
+
#
|
6
|
+
# This constraint verifies that a collection contains an expected value by using
|
7
|
+
# the collection's #include? method. It works with any object that responds to
|
8
|
+
# #include?, such as Arrays, Sets, Strings, and Ranges.
|
9
|
+
#
|
10
|
+
# @example Array inclusion
|
11
|
+
# constraint = InclusionConstraint.new(:self, 2)
|
12
|
+
# constraint.satisfied?([1, 2, 3]) # => true
|
13
|
+
# constraint.satisfied?([1, 3, 4]) # => false
|
14
|
+
#
|
15
|
+
# @example String inclusion
|
16
|
+
# constraint = InclusionConstraint.new(:self, 'b')
|
17
|
+
# constraint.satisfied?('abc') # => true
|
18
|
+
# constraint.satisfied?('ac') # => false
|
19
|
+
#
|
20
|
+
# @example Range inclusion
|
21
|
+
# constraint = InclusionConstraint.new(:self, 5)
|
22
|
+
# constraint.satisfied?(1..10) # => true
|
23
|
+
# constraint.satisfied?(11..20) # => false
|
24
|
+
#
|
25
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
26
|
+
# @since 0.1.0
|
27
|
+
class InclusionConstraint
|
28
|
+
include Behavior[untyped, Enumerable[untyped], { }]
|
29
|
+
|
30
|
+
# Get a human-readable description of the inclusion requirement.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# constraint = InclusionConstraint.new(:self, 42)
|
34
|
+
# constraint.short_description # => "including 42"
|
35
|
+
#
|
36
|
+
# @return [String] A description of the inclusion requirement
|
37
|
+
def short_description: ...
|
38
|
+
|
39
|
+
# Get a human-readable description of why inclusion validation failed.
|
40
|
+
#
|
41
|
+
# @example
|
42
|
+
# constraint = InclusionConstraint.new(:self, 42)
|
43
|
+
# constraint.satisfied?([1, 2, 3])
|
44
|
+
# constraint.short_violation_description # => 'excluding 42'
|
45
|
+
#
|
46
|
+
# @return [String] A description of which value was missing
|
47
|
+
def short_violation_description: ...
|
48
|
+
|
49
|
+
# Check if the collection includes the expected value.
|
50
|
+
#
|
51
|
+
# Uses the collection's #include? method to verify that the expected
|
52
|
+
# value is present in the collection being validated.
|
53
|
+
#
|
54
|
+
# @return [Boolean] true if the collection includes the expected value
|
55
|
+
def satisfies_constraint?: ...
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint for validating that strings match a given pattern.
|
5
|
+
#
|
6
|
+
# This constraint verifies that a string value matches a specified regular expression
|
7
|
+
# pattern. It supports both Regexp objects and string patterns that are converted to
|
8
|
+
# regular expressions.
|
9
|
+
#
|
10
|
+
# @example Basic pattern matching
|
11
|
+
# constraint = MatchPatternConstraint.new(:self, /\A\d+\z/)
|
12
|
+
# constraint.satisfied?("123") # => true
|
13
|
+
# constraint.satisfied?("abc") # => false
|
14
|
+
#
|
15
|
+
# @example String pattern conversion
|
16
|
+
# constraint = MatchPatternConstraint.new(:self, '\A\w+@\w+\.\w+\z')
|
17
|
+
# constraint.satisfied?("test@example.com") # => true
|
18
|
+
# constraint.satisfied?("invalid-email") # => false
|
19
|
+
#
|
20
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
21
|
+
# @since 0.1.0
|
22
|
+
class MatchPatternConstraint
|
23
|
+
include Behavior[Regexp, untyped, { }]
|
24
|
+
|
25
|
+
# Get a human-readable description of the pattern requirement.
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# constraint = MatchPatternConstraint.new(:self, /\d+/)
|
29
|
+
# constraint.short_description # => "matches /\\d+/"
|
30
|
+
#
|
31
|
+
# @return [String] A description of the pattern requirement
|
32
|
+
def short_description: ...
|
33
|
+
|
34
|
+
# Get a human-readable description of why pattern validation failed.
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# constraint = MatchPatternConstraint.new(:self, /\d+/)
|
38
|
+
# constraint.satisfied?("abc")
|
39
|
+
# constraint.short_violation_description # => "does not match /\\d+/"
|
40
|
+
#
|
41
|
+
# @return [String] A description of the pattern mismatch
|
42
|
+
def short_violation_description: ...
|
43
|
+
|
44
|
+
# Coerce string patterns into Regexp objects.
|
45
|
+
#
|
46
|
+
# @param expectation [String, Regexp] The pattern to coerce
|
47
|
+
#
|
48
|
+
# @return [Regexp] The coerced pattern
|
49
|
+
def coerce_expectation: ...
|
50
|
+
|
51
|
+
# Check if the value matches the expected pattern.
|
52
|
+
#
|
53
|
+
# @return [Boolean] true if the value matches the pattern
|
54
|
+
def satisfies_constraint?: ...
|
55
|
+
|
56
|
+
# Validate that the expectation is a valid regular expression.
|
57
|
+
#
|
58
|
+
# @param expectation [Object] The pattern to validate
|
59
|
+
#
|
60
|
+
# @raise [ArgumentError] if the expectation is not a Regexp
|
61
|
+
# @return [void]
|
62
|
+
def validate_expectation!: ...
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint that validates whether an object responds to a specified method
|
5
|
+
#
|
6
|
+
# This constraint checks if an object implements a particular interface by verifying
|
7
|
+
# it responds to a given method name. The method name must be provided as a Symbol.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# constraint = MethodPresenceConstraint.new(:to_s)
|
11
|
+
# constraint.satisfied_by?(Object.new) # => true
|
12
|
+
# constraint.satisfied_by?(BasicObject.new) # => false
|
13
|
+
#
|
14
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
15
|
+
# @since 0.1.0
|
16
|
+
class MethodPresenceConstraint
|
17
|
+
include Behavior[Symbol, untyped, { }]
|
18
|
+
|
19
|
+
# Get a short description of what this constraint expects
|
20
|
+
#
|
21
|
+
# @return [String] description of the expected method
|
22
|
+
def short_description: ...
|
23
|
+
|
24
|
+
# Get a short description of why the constraint was violated
|
25
|
+
#
|
26
|
+
# @return [String] description of the missing method
|
27
|
+
def short_violation_description: ...
|
28
|
+
|
29
|
+
# Coerce the expectation into a symbol
|
30
|
+
#
|
31
|
+
# @param expectation [Symbol] the method name to check
|
32
|
+
#
|
33
|
+
# @return [Symbol] coerced method name
|
34
|
+
def coerce_expectation: ...
|
35
|
+
|
36
|
+
# Check if the actual value satisfies the constraint
|
37
|
+
#
|
38
|
+
# @return [Boolean] true if the object responds to the expected method
|
39
|
+
def satisfies_constraint?: ...
|
40
|
+
|
41
|
+
# Validate that the expectation is a Symbol
|
42
|
+
#
|
43
|
+
# @param expectation [Object] the expectation to validate
|
44
|
+
#
|
45
|
+
# @raise [ArgumentError] if the expectation is not a Symbol
|
46
|
+
# @return [void]
|
47
|
+
def validate_expectation!: ...
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint that ensures no element in an enumerable satisfies a given constraint.
|
5
|
+
#
|
6
|
+
# The NoneConstraint validates that none of the elements in an enumerable value meet
|
7
|
+
# the provided constraint. This enables validation rules like "must not contain any
|
8
|
+
# strings" or "must have no negative numbers".
|
9
|
+
#
|
10
|
+
# Key features:
|
11
|
+
# - Validates enumerable elements against a constraint
|
12
|
+
# - Short-circuits on first violating element
|
13
|
+
# - Provides clear error messages for violating elements
|
14
|
+
# - Handles non-enumerable values gracefully
|
15
|
+
#
|
16
|
+
# @example Validating an array contains no strings
|
17
|
+
# string_constraint = StringConstraint.new(:self)
|
18
|
+
# no_strings = NoneConstraint.new(:self, string_constraint)
|
19
|
+
#
|
20
|
+
# no_strings.satisfied?([1, 2, 3]) # => true
|
21
|
+
# no_strings.satisfied?(['a', 1, 'c']) # => false
|
22
|
+
# no_strings.satisfied?(nil) # => false (not enumerable)
|
23
|
+
#
|
24
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
25
|
+
# @since 0.1.0
|
26
|
+
class NoneConstraint
|
27
|
+
include Behavior[Behavior[untyped, untyped, untyped], Enumerable, { }]
|
28
|
+
|
29
|
+
# Get a description of what the constraint expects.
|
30
|
+
#
|
31
|
+
# @return [String] a description negating the expected constraint description
|
32
|
+
def short_description: ...
|
33
|
+
|
34
|
+
# The description of the violations that caused the constraint to be unsatisfied.
|
35
|
+
#
|
36
|
+
# This method provides detailed feedback when any constraints are satisfied,
|
37
|
+
# listing all the ways in which the value failed validation.
|
38
|
+
#
|
39
|
+
# @return [String] The combined violation descriptions from all violating elements
|
40
|
+
def short_violation_description: ...
|
41
|
+
|
42
|
+
# Check if the value satisfies none of the expected constraints.
|
43
|
+
#
|
44
|
+
# @return [Boolean] whether no constraints are satisfied
|
45
|
+
def satisfies_constraint?: ...
|
46
|
+
|
47
|
+
# Validate that the expectation is a valid constraint.
|
48
|
+
#
|
49
|
+
# @param expectation [Object] the expectation to validate
|
50
|
+
#
|
51
|
+
# @raise [ArgumentError] if the expectation is not a valid Domainic::Type::Constraint
|
52
|
+
# @return [void]
|
53
|
+
def validate_expectation!: ...
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint that ensures none of the provided constraints are satisfied.
|
5
|
+
#
|
6
|
+
# The NorConstraint validates that none of the elements in an enumerable value meet
|
7
|
+
# any of the provided constraints. This enables validation rules like "must not contain
|
8
|
+
# any strings or negative numbers".
|
9
|
+
#
|
10
|
+
# Key features:
|
11
|
+
# - Validates enumerable elements against multiple constraints with NOR logic
|
12
|
+
# - Short-circuits on first satisfying constraint for performance
|
13
|
+
# - Provides clear error messages for violating constraints
|
14
|
+
# - Handles non-enumerable values gracefully
|
15
|
+
# - Supports incremental constraint addition
|
16
|
+
#
|
17
|
+
# @example Validating an array contains no strings or negative numbers
|
18
|
+
# string_constraint = StringConstraint.new(:self)
|
19
|
+
# negative_number_constraint = NegativeNumberConstraint.new(:self)
|
20
|
+
# no_strings_or_negatives = NorConstraint.new(:self, [string_constraint, negative_number_constraint])
|
21
|
+
#
|
22
|
+
# no_strings_or_negatives.satisfied?([1, 2, 3]) # => true
|
23
|
+
# no_strings_or_negatives.satisfied?(['a', 1, 'c']) # => false
|
24
|
+
# no_strings_or_negatives.satisfied?([1, -2, 3]) # => false
|
25
|
+
# no_strings_or_negatives.satisfied?(nil) # => false (not enumerable)
|
26
|
+
#
|
27
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
28
|
+
# @since 0.1.0
|
29
|
+
class NorConstraint
|
30
|
+
include Behavior[Array[Behavior[untyped, untyped, untyped]], untyped, { }]
|
31
|
+
|
32
|
+
# Get a description of what the constraint expects.
|
33
|
+
#
|
34
|
+
# @return [String] a description combining all constraint descriptions with 'nor'
|
35
|
+
def short_description: ...
|
36
|
+
|
37
|
+
# The description of the violations that caused the constraint to be unsatisfied.
|
38
|
+
#
|
39
|
+
# This method provides detailed feedback about which constraints were satisfied,
|
40
|
+
# listing all violations that caused the validation to fail.
|
41
|
+
#
|
42
|
+
# @return [String] The combined violation descriptions from all satisfied constraints
|
43
|
+
def short_violation_description: ...
|
44
|
+
|
45
|
+
# Coerce the expectation into an array and append new constraints.
|
46
|
+
#
|
47
|
+
# This enables both initializing with an array of constraints and adding
|
48
|
+
# new constraints incrementally via expecting().
|
49
|
+
#
|
50
|
+
# @param expectation [Behavior] the constraint to add
|
51
|
+
#
|
52
|
+
# @return [Array<Behavior>] the updated array of constraints
|
53
|
+
def coerce_expectation: (untyped expectation) -> Array[Behavior[untyped, untyped, untyped]]
|
54
|
+
|
55
|
+
# Check if none of the values satisfy any of the expected constraints.
|
56
|
+
#
|
57
|
+
# Short-circuits on the first satisfying constraint for efficiency.
|
58
|
+
#
|
59
|
+
# @return [Boolean] whether none of the constraints are satisfied
|
60
|
+
def satisfies_constraint?: ...
|
61
|
+
|
62
|
+
# Validate that the expectation is an array of valid constraints.
|
63
|
+
#
|
64
|
+
# @param expectation [Object] the expectation to validate
|
65
|
+
#
|
66
|
+
# @raise [ArgumentError] if the expectation is not a valid Domainic::Type::Constraint
|
67
|
+
# @return [void]
|
68
|
+
def validate_expectation!: ...
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint that negates another constraint's validation logic.
|
5
|
+
#
|
6
|
+
# The NotConstraint inverts the validation result of its inner constraint,
|
7
|
+
# allowing for negative validation rules. This enables expressing conditions
|
8
|
+
# like "not equal to", "not included in", etc.
|
9
|
+
#
|
10
|
+
# Key features:
|
11
|
+
# - Inverts any constraint's validation logic
|
12
|
+
# - Maintains clear error messages with proper negation
|
13
|
+
# - Can be composed with other logical constraints
|
14
|
+
# - Preserves the original constraint's type safety
|
15
|
+
#
|
16
|
+
# @example Validating a value is not a specific type
|
17
|
+
# string_constraint = StringConstraint.new(:self)
|
18
|
+
# not_string = NotConstraint.new(:self, string_constraint)
|
19
|
+
#
|
20
|
+
# not_string.satisfied?(123) # => true
|
21
|
+
# not_string.satisfied?("test") # => false
|
22
|
+
#
|
23
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
24
|
+
# @since 0.1.0
|
25
|
+
class NotConstraint
|
26
|
+
include Behavior[Behavior[untyped, untyped, untyped], { }, { }]
|
27
|
+
|
28
|
+
# Get a description of what the constraint expects.
|
29
|
+
#
|
30
|
+
# @return [String] the negated constraint description
|
31
|
+
def short_description: ...
|
32
|
+
|
33
|
+
# The description of the violations that caused the constraint to be unsatisfied.
|
34
|
+
#
|
35
|
+
# This is used to help compose a error message when the constraint is not satisfied.
|
36
|
+
# Implementing classes can override this to provide more specific failure messages.
|
37
|
+
#
|
38
|
+
# @return [String] The description of the constraint when it fails.
|
39
|
+
def short_violation_description: ...
|
40
|
+
|
41
|
+
# Check if the value does not satisfy the expected constraint.
|
42
|
+
#
|
43
|
+
# @return [Boolean] whether the constraint is satisfied
|
44
|
+
def satisfies_constraint?: ...
|
45
|
+
|
46
|
+
# Validate that the expectation is a valid constraint.
|
47
|
+
#
|
48
|
+
# @param expectation [Object] the expectation to validate
|
49
|
+
#
|
50
|
+
# @raise [ArgumentError] if the expectation is not a valid constraint
|
51
|
+
# @return [void]
|
52
|
+
def validate_expectation!: ...
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A constraint that combines multiple constraints with logical OR behavior.
|
5
|
+
#
|
6
|
+
# The OrConstraint validates that a value satisfies at least one of its provided constraints,
|
7
|
+
# implementing logical OR behavior. This enables validation rules like "must be either a
|
8
|
+
# string or a symbol" or "must be nil or a positive number".
|
9
|
+
#
|
10
|
+
# Key features:
|
11
|
+
# - Combines multiple constraints with OR logic
|
12
|
+
# - Short-circuits on first passing constraint
|
13
|
+
# - Provides clear error messages listing all failed attempts
|
14
|
+
# - Supports incremental constraint addition
|
15
|
+
#
|
16
|
+
# @example Validating a value is either a String or Symbol
|
17
|
+
# string_constraint = StringConstraint.new(:self)
|
18
|
+
# symbol_constraint = SymbolConstraint.new(:self)
|
19
|
+
# string_or_symbol = OrConstraint.new(:self, [string_constraint, symbol_constraint])
|
20
|
+
#
|
21
|
+
# string_or_symbol.satisfied?("test") # => true
|
22
|
+
# string_or_symbol.satisfied?(:test) # => true
|
23
|
+
# string_or_symbol.satisfied?(123) # => false
|
24
|
+
#
|
25
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
26
|
+
# @since 0.1.0
|
27
|
+
class OrConstraint
|
28
|
+
include Behavior[Array[Behavior[untyped, untyped, untyped]], untyped, { }]
|
29
|
+
|
30
|
+
# Get a description of what the constraint expects.
|
31
|
+
#
|
32
|
+
# @return [String] a description combining all constraint descriptions with 'or'
|
33
|
+
def short_description: ...
|
34
|
+
|
35
|
+
def expecting: (Behavior[untyped, untyped, untyped]) -> self
|
36
|
+
|
37
|
+
# The description of the violations that caused the constraint to be unsatisfied.
|
38
|
+
#
|
39
|
+
# This method provides detailed feedback when no constraints are satisfied,
|
40
|
+
# listing all the ways in which the value failed validation. Uses 'and' in the
|
41
|
+
# message because all constraints failed (e.g., "was not a string AND was not a
|
42
|
+
# symbol" rather than "was not a string OR was not a symbol").
|
43
|
+
#
|
44
|
+
# @return [String] The combined violation descriptions from all constraints
|
45
|
+
def short_violation_description: ...
|
46
|
+
|
47
|
+
# Coerce the expectation into an array and append new constraints.
|
48
|
+
#
|
49
|
+
# This enables both initializing with an array of constraints and adding
|
50
|
+
# new constraints incrementally via expecting().
|
51
|
+
#
|
52
|
+
# @param expectation [Behavior] the constraint to add
|
53
|
+
#
|
54
|
+
# @return [Array<Behavior>] the updated array of constraints
|
55
|
+
def coerce_expectation: (untyped expectation) -> Array[Behavior[untyped, untyped, untyped]]
|
56
|
+
|
57
|
+
# Check if the value satisfies any of the expected constraints.
|
58
|
+
#
|
59
|
+
# Short-circuits on the first satisfied constraint for efficiency.
|
60
|
+
#
|
61
|
+
# @return [Boolean] whether any constraint is satisfied
|
62
|
+
def satisfies_constraint?: ...
|
63
|
+
|
64
|
+
# Validate that the expectation is an array of valid constraints.
|
65
|
+
#
|
66
|
+
# @param expectation [Object] the expectation to validate
|
67
|
+
#
|
68
|
+
# @raise [ArgumentError] if the expectation is not valid
|
69
|
+
# @return [void]
|
70
|
+
def validate_expectation!: ...
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|