expectant 0.1.0 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +128 -20
- data/lib/expectant/bound_schema.rb +81 -0
- data/lib/expectant/configuration.rb +12 -0
- data/lib/expectant/dsl.rb +119 -77
- data/lib/expectant/errors.rb +8 -0
- data/lib/expectant/expectation.rb +37 -66
- data/lib/expectant/schema.rb +94 -0
- data/lib/expectant/types.rb +56 -0
- data/lib/expectant/utils.rb +46 -0
- data/lib/expectant/version.rb +1 -1
- data/lib/expectant.rb +17 -5
- metadata +42 -8
- data/CODE_OF_CONDUCT.md +0 -132
- data/lib/expectant/schema_builder.rb +0 -86
- data/sig/expectant.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cd084f7dbdd4fa98ec53ec0cfd58a5c2f9ade3a0eeebec5ea992cb30f581482
|
4
|
+
data.tar.gz: 49f931b7b2ee7633406a87f165691245428c8542de9f90f7278f72cd711e04c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 763ab6b831344ba33a679f32ba5df7469bc2d9d985a8e75483a1077ba1f65f5a1abe5e1af571a76b2af1aca7fcd9b4601e1fee066ffbd5627f0c3c2f69904577
|
7
|
+
data.tar.gz: f91ca026879484b537c166883e55e4011856812baa38f59e3f2830f6fc7d761a06d13b538efc3271a662313b199036ec5e85b3196de0a595f990b555b4570055
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License (MIT)
|
2
2
|
|
3
|
-
Copyright (c) 2025
|
3
|
+
Copyright (c) 2025 Hector Medina Fetterman
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -1,43 +1,151 @@
|
|
1
1
|
# Expectant
|
2
2
|
|
3
|
-
|
3
|
+
[](https://rubygems.org/gems/expectant)
|
4
4
|
|
5
|
-
|
5
|
+
A Ruby DSL for defining validation schemas with type coercion, defaults, and fallbacks. Built on [dry-validation](https://dry-rb.org/gems/dry-validation/) and [dry-types](https://dry-rb.org/gems/dry-types/).
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'expectant'
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
## Quick Start
|
12
14
|
|
13
|
-
```
|
14
|
-
|
15
|
+
```ruby
|
16
|
+
class InvoiceService
|
17
|
+
include Expectant::DSL
|
18
|
+
|
19
|
+
expects :inputs
|
20
|
+
|
21
|
+
input :customer_id, type: :integer
|
22
|
+
input :amount, type: :float
|
23
|
+
input :status, type: :string, default: "draft"
|
24
|
+
input :description, type: :string, optional: true
|
25
|
+
|
26
|
+
input_rule(:amount) do
|
27
|
+
key.failure("must be positive") if value && value <= 0
|
28
|
+
end
|
29
|
+
|
30
|
+
def process(data)
|
31
|
+
result = inputs.validate(data)
|
32
|
+
|
33
|
+
if result.success?
|
34
|
+
# Use validated data: result.to_h
|
35
|
+
else
|
36
|
+
# Handle errors: result.errors.to_h
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
15
40
|
```
|
16
41
|
|
17
|
-
|
42
|
+
## Core Features
|
18
43
|
|
19
|
-
|
20
|
-
|
44
|
+
### Types
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
expects :inputs
|
48
|
+
|
49
|
+
input :name, type: :string
|
50
|
+
input :age, type: :integer
|
51
|
+
input :price, type: :float
|
52
|
+
input :active, type: :boolean
|
53
|
+
input :date, type: :date
|
54
|
+
input :time, type: :datetime
|
55
|
+
input :data, type: :hash
|
56
|
+
input :tags, type: :array
|
57
|
+
input :user, type: User # Custom class
|
21
58
|
```
|
22
59
|
|
23
|
-
|
60
|
+
### Defaults and Optional Fields
|
24
61
|
|
25
|
-
|
62
|
+
```ruby
|
63
|
+
# Optional (can be omitted)
|
64
|
+
input :description, type: :string, optional: true
|
26
65
|
|
27
|
-
|
66
|
+
# Static default
|
67
|
+
input :status, type: :string, default: "draft"
|
28
68
|
|
29
|
-
|
69
|
+
# Dynamic default (proc)
|
70
|
+
input :created_at, type: :datetime, default: -> { Time.now }
|
71
|
+
```
|
30
72
|
|
31
|
-
|
73
|
+
### Fallbacks
|
32
74
|
|
33
|
-
|
75
|
+
Automatically substitute values when validation fails:
|
34
76
|
|
35
|
-
|
77
|
+
```ruby
|
78
|
+
input :per_page, type: :integer, default: 25, fallback: 25
|
36
79
|
|
37
|
-
|
80
|
+
input_rule(:per_page) do
|
81
|
+
key.failure("max 100") if value && value > 100
|
82
|
+
end
|
83
|
+
|
84
|
+
# If per_page: 500 is provided, validation succeeds with fallback value 25
|
85
|
+
```
|
86
|
+
|
87
|
+
### Validation Rules
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Single field
|
91
|
+
input_rule(:email) do
|
92
|
+
key.failure("invalid") unless value&.include?("@")
|
93
|
+
end
|
94
|
+
|
95
|
+
# Multiple fields
|
96
|
+
input_rule(:start_date, :end_date) do
|
97
|
+
base.failure("start must be before end") if values[:start_date] > values[:end_date]
|
98
|
+
end
|
99
|
+
|
100
|
+
# Global rule
|
101
|
+
input_rule do
|
102
|
+
base.failure("error") if some_condition
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
### Context
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
input_rule(:order) do
|
110
|
+
valid = context[:orderable_columns] || []
|
111
|
+
key.failure("invalid column") unless valid.include?(value)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Pass context when validating
|
115
|
+
inputs.validate(data, context: { orderable_columns: ["id", "name"] })
|
116
|
+
```
|
117
|
+
|
118
|
+
### Multiple Schemas
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
expects :inputs
|
122
|
+
expects :outputs
|
123
|
+
|
124
|
+
input :data, type: :hash
|
125
|
+
output :result, type: :string
|
126
|
+
|
127
|
+
inputs.validate(input_data)
|
128
|
+
outputs.validate(output_data)
|
129
|
+
```
|
38
130
|
|
39
|
-
|
131
|
+
## Configuration
|
40
132
|
|
41
|
-
|
133
|
+
Customize validator method names:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
Expectant.configure do |config|
|
137
|
+
config.validator_suffix = "validation" # input_validation instead of input_rule
|
138
|
+
config.validator_prefix = "validate" # validate_input
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
## Development
|
143
|
+
|
144
|
+
```bash
|
145
|
+
bundle install
|
146
|
+
bundle exec rspec
|
147
|
+
```
|
148
|
+
|
149
|
+
## License
|
42
150
|
|
43
|
-
|
151
|
+
MIT License
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Expectant
|
4
|
+
# Instance-bound schema wrapper that provides validation with instance context
|
5
|
+
class BoundSchema
|
6
|
+
def initialize(instance, schema)
|
7
|
+
@instance = instance
|
8
|
+
@schema = schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(data, context: {})
|
12
|
+
# Apply proc defaults before validation
|
13
|
+
data = apply_defaults(data)
|
14
|
+
|
15
|
+
# Create contract instance with context, then validate
|
16
|
+
contract_class = @schema.contract
|
17
|
+
contract = contract_class.new(instance: @instance, context: context)
|
18
|
+
result = contract.call(data)
|
19
|
+
|
20
|
+
# If validation failed, apply fallbacks for fields with errors and retry
|
21
|
+
if !result.success?
|
22
|
+
data_with_fallbacks = apply_fallbacks(data, result)
|
23
|
+
if data_with_fallbacks != data
|
24
|
+
# Re-validate with fallback values
|
25
|
+
result = contract.call(data_with_fallbacks)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
def keys
|
33
|
+
@schema.keys
|
34
|
+
end
|
35
|
+
|
36
|
+
def contract
|
37
|
+
@schema.contract
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Apply default values for missing fields (especially proc defaults)
|
43
|
+
def apply_defaults(data)
|
44
|
+
data = data.dup
|
45
|
+
|
46
|
+
@schema.fields.each do |field|
|
47
|
+
# Skip if value already provided
|
48
|
+
next if data.key?(field.name)
|
49
|
+
|
50
|
+
# Apply proc defaults, static defaults are handled by dry-types
|
51
|
+
if field.default.respond_to?(:call)
|
52
|
+
data[field.name] = @instance.instance_exec(&field.default)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
data
|
57
|
+
end
|
58
|
+
|
59
|
+
# Apply fallback values to fields that have errors
|
60
|
+
def apply_fallbacks(data, result)
|
61
|
+
data = data.dup
|
62
|
+
|
63
|
+
@schema.fields.each do |field|
|
64
|
+
next unless field.has_fallback?
|
65
|
+
|
66
|
+
# Apply fallback if field has an error
|
67
|
+
if result.errors[field.name]&.any?
|
68
|
+
fallback_value = if field.fallback.respond_to?(:call)
|
69
|
+
# Proc fallback - evaluate in instance context
|
70
|
+
@instance.instance_exec(&field.fallback)
|
71
|
+
else
|
72
|
+
field.fallback
|
73
|
+
end
|
74
|
+
data[field.name] = fallback_value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
data
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Expectant
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :validator_prefix, :validator_suffix
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@validator_prefix = nil # default: no prefix
|
9
|
+
@validator_suffix = "rule" # default: schema_name_rule
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/expectant/dsl.rb
CHANGED
@@ -1,77 +1,119 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
definition
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "utils"
|
4
|
+
require_relative "schema"
|
5
|
+
require_relative "bound_schema"
|
6
|
+
|
7
|
+
module Expectant
|
8
|
+
module DSL
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def self.extended(base)
|
15
|
+
base.class_eval do
|
16
|
+
@_expectant_schemas = {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def inherited(sub)
|
21
|
+
return unless instance_variable_defined?(:@_expectant_schemas)
|
22
|
+
return if @_expectant_schemas.empty?
|
23
|
+
|
24
|
+
# Deep copy each schema (fields + validators)
|
25
|
+
parent_schemas = @_expectant_schemas
|
26
|
+
duped = parent_schemas.transform_values { |schema| schema.duplicate }
|
27
|
+
|
28
|
+
sub.instance_variable_set(:@_expectant_schemas, duped)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Define a new expectation schema
|
32
|
+
# Options:
|
33
|
+
# collision: :error|:force -> method name collision policy for dynamic definitions
|
34
|
+
# singular: string|symbol -> control singular name for the schema
|
35
|
+
def expects(schema_name, collision: :error, singular: nil)
|
36
|
+
field_method_name = Utils.singularize(schema_name.to_sym)
|
37
|
+
if !singular.nil?
|
38
|
+
if singular.is_a?(String) || singular.is_a?(Symbol)
|
39
|
+
field_method_name = singular.to_sym
|
40
|
+
else
|
41
|
+
raise ConfigurationError, "Invalid singular option: #{singular.inspect}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
schema = schema_name.to_sym
|
45
|
+
|
46
|
+
if @_expectant_schemas.key?(schema)
|
47
|
+
raise SchemaError, "Schema :#{schema} already defined"
|
48
|
+
else
|
49
|
+
create_schema(schema, collision: collision, field_method_name: field_method_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def reset_inherited_expectations!
|
56
|
+
@_expectant_schemas = {}
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def create_schema(schema_name, collision: :error, field_method_name: nil)
|
62
|
+
@_expectant_schemas[schema_name] = Schema.new(schema_name)
|
63
|
+
|
64
|
+
# Dynamically define the field definition method
|
65
|
+
# (e.g. input for :inputs, datum for :data)
|
66
|
+
Utils.define_with_collision_policy(singleton_class, field_method_name, collision: collision) do
|
67
|
+
define_singleton_method(field_method_name) do |name, **options|
|
68
|
+
expectation = Expectation.new(name, **options)
|
69
|
+
@_expectant_schemas[schema_name].add_field(expectation)
|
70
|
+
expectation
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Reset a schema
|
75
|
+
reset_method_name = "reset_#{schema_name}!"
|
76
|
+
Utils.define_with_collision_policy(singleton_class, reset_method_name, collision: collision) do
|
77
|
+
define_singleton_method(reset_method_name) do
|
78
|
+
@_expectant_schemas[schema_name].reset!
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Define validators
|
83
|
+
method_name = Utils.validator_method_name(field_method_name, Expectant.configuration)
|
84
|
+
Utils.define_with_collision_policy(singleton_class, method_name, collision: collision) do
|
85
|
+
define_singleton_method(method_name) do |*field_names, &block|
|
86
|
+
@_expectant_schemas[schema_name].add_validator({
|
87
|
+
name: if field_names.empty?
|
88
|
+
nil
|
89
|
+
else
|
90
|
+
((field_names.size == 1) ? field_names.first : field_names)
|
91
|
+
end,
|
92
|
+
block: block
|
93
|
+
})
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Add class-level schema accessor method (e.g. MyClass.inputs)
|
98
|
+
Utils.define_with_collision_policy(singleton_class, schema_name, collision: collision) do
|
99
|
+
define_singleton_method(schema_name) do
|
100
|
+
@_expectant_schemas[schema_name]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Add instance-level schema accessor method (e.g. instance.inputs)
|
105
|
+
if !instance_methods(false).include?(schema_name)
|
106
|
+
define_method(schema_name) do
|
107
|
+
BoundSchema.new(self, self.class.instance_variable_get(:@_expectant_schemas)[schema_name])
|
108
|
+
end
|
109
|
+
elsif collision == :force
|
110
|
+
define_method(schema_name) do
|
111
|
+
BoundSchema.new(self, self.class.instance_variable_get(:@_expectant_schemas)[schema_name])
|
112
|
+
end
|
113
|
+
elsif collision == :error
|
114
|
+
raise ConfigurationError, "Instance method #{schema_name} already defined"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -1,66 +1,37 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Expectant
|
4
|
-
class Expectation
|
5
|
-
attr_reader :name, :type, :
|
6
|
-
|
7
|
-
def initialize(name, type: nil, optional: false,
|
8
|
-
@name = name
|
9
|
-
@type = type
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@options = options
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
type
|
39
|
-
else
|
40
|
-
nil
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def dry_validation_type
|
45
|
-
case type
|
46
|
-
when :integer, :int
|
47
|
-
:integer
|
48
|
-
when :float
|
49
|
-
:float
|
50
|
-
when :string, :str
|
51
|
-
:string
|
52
|
-
when :boolean, :bool
|
53
|
-
:bool
|
54
|
-
when :array
|
55
|
-
:array
|
56
|
-
when :hash
|
57
|
-
:hash
|
58
|
-
when Class
|
59
|
-
# For custom classes, we'll use :any and add custom validation
|
60
|
-
:any
|
61
|
-
else
|
62
|
-
:any
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Expectant
|
4
|
+
class Expectation
|
5
|
+
attr_reader :name, :type, :dry_type, :fallback, :default
|
6
|
+
|
7
|
+
def initialize(name, type: nil, default: nil, optional: false, fallback: nil, **options)
|
8
|
+
@name = name
|
9
|
+
@type = type
|
10
|
+
@default = default
|
11
|
+
@fallback = fallback
|
12
|
+
@options = options
|
13
|
+
|
14
|
+
base_type = Types.resolve(type)
|
15
|
+
base_type = base_type.optional if optional
|
16
|
+
# if default is a proc, we call it at runtime
|
17
|
+
@dry_type = if !default.nil? && !default.respond_to?(:call)
|
18
|
+
base_type.default(default)
|
19
|
+
else
|
20
|
+
base_type
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# A field is required if it's not optional and has no default
|
25
|
+
def required?
|
26
|
+
!@dry_type.optional? && !has_default?
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_default?
|
30
|
+
@dry_type.default? || !@default.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_fallback?
|
34
|
+
!@fallback.nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/validation"
|
4
|
+
|
5
|
+
module Expectant
|
6
|
+
class Schema
|
7
|
+
attr_reader :name, :fields, :validators
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
@fields = []
|
12
|
+
@validators = []
|
13
|
+
@contract_class = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def keys
|
17
|
+
@fields.map(&:name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def contract
|
21
|
+
@contract_class ||= build_contract
|
22
|
+
end
|
23
|
+
|
24
|
+
def duplicate
|
25
|
+
dup = self.class.new(@name)
|
26
|
+
dup.instance_variable_set(:@fields, @fields.dup)
|
27
|
+
dup.instance_variable_set(:@validators, @validators.dup)
|
28
|
+
dup
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_field(expectation)
|
32
|
+
@fields << expectation
|
33
|
+
@contract_class = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_validator(validator_def)
|
37
|
+
@validators << validator_def
|
38
|
+
@contract_class = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def freeze
|
42
|
+
@fields.freeze
|
43
|
+
@validators.freeze
|
44
|
+
super
|
45
|
+
end
|
46
|
+
|
47
|
+
def reset!
|
48
|
+
@fields = []
|
49
|
+
@validators = []
|
50
|
+
@contract_class = nil
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def build_contract
|
56
|
+
fields = @fields
|
57
|
+
validators = @validators
|
58
|
+
|
59
|
+
Class.new(Dry::Validation::Contract) do
|
60
|
+
# Enable option passing (allows context and instance to be passed at validation time)
|
61
|
+
option :context, default: proc { {} }
|
62
|
+
option :instance, optional: true
|
63
|
+
|
64
|
+
# Define schema based on fields using dry-types
|
65
|
+
params do
|
66
|
+
fields.each do |field|
|
67
|
+
dry_type = field.dry_type
|
68
|
+
|
69
|
+
if field.required?
|
70
|
+
required(field.name).value(dry_type)
|
71
|
+
else
|
72
|
+
optional(field.name).value(dry_type)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Add custom validators
|
78
|
+
validators.each do |validator_def|
|
79
|
+
if validator_def[:name]
|
80
|
+
# Single key or multiple keys
|
81
|
+
if validator_def[:name].is_a?(Array)
|
82
|
+
rule(*validator_def[:name], &validator_def[:block])
|
83
|
+
else
|
84
|
+
rule(validator_def[:name], &validator_def[:block])
|
85
|
+
end
|
86
|
+
else
|
87
|
+
# Global rule without a specific field
|
88
|
+
rule(&validator_def[:block])
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry-types"
|
4
|
+
|
5
|
+
module Expectant
|
6
|
+
module Types
|
7
|
+
include Dry.Types()
|
8
|
+
|
9
|
+
def self.resolve(type_definition)
|
10
|
+
case type_definition
|
11
|
+
when nil
|
12
|
+
Any
|
13
|
+
when Symbol
|
14
|
+
resolve_symbol(type_definition)
|
15
|
+
when Dry::Types::Type
|
16
|
+
# Already a dry-type, return as-is
|
17
|
+
type_definition
|
18
|
+
when Class
|
19
|
+
Instance(type_definition)
|
20
|
+
else
|
21
|
+
raise ConfigurationError, "Invalid type definition: #{type_definition}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.resolve_symbol(symbol)
|
26
|
+
case symbol
|
27
|
+
when :string, :str
|
28
|
+
Params::String
|
29
|
+
when :integer, :int
|
30
|
+
Params::Integer
|
31
|
+
when :float
|
32
|
+
Params::Float
|
33
|
+
when :decimal
|
34
|
+
Params::Decimal
|
35
|
+
when :boolean, :bool
|
36
|
+
Params::Bool
|
37
|
+
when :date
|
38
|
+
Params::Date
|
39
|
+
when :datetime
|
40
|
+
Params::DateTime
|
41
|
+
when :time
|
42
|
+
Params::Time
|
43
|
+
when :array
|
44
|
+
Params::Array
|
45
|
+
when :hash
|
46
|
+
Params::Hash
|
47
|
+
when :symbol, :sym
|
48
|
+
Symbol
|
49
|
+
when :any, :nil
|
50
|
+
Any
|
51
|
+
else
|
52
|
+
raise ConfigurationError, "Unknown type symbol: #{symbol}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/inflector"
|
4
|
+
|
5
|
+
module Expectant
|
6
|
+
module Utils
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def singularize(word)
|
10
|
+
ActiveSupport::Inflector.singularize(word.to_s).to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def validator_method_name(schema_name, configuration)
|
14
|
+
prefix = configuration.validator_prefix
|
15
|
+
suffix = configuration.validator_suffix
|
16
|
+
|
17
|
+
parts = []
|
18
|
+
parts << prefix if prefix
|
19
|
+
parts << schema_name
|
20
|
+
parts << suffix if suffix
|
21
|
+
|
22
|
+
parts.join("_")
|
23
|
+
end
|
24
|
+
|
25
|
+
def define_with_collision_policy(target, method_name, collision:, &block)
|
26
|
+
method_name = method_name.to_sym
|
27
|
+
if target.method_defined?(method_name) || target.private_method_defined?(method_name)
|
28
|
+
case collision
|
29
|
+
when :error
|
30
|
+
raise ConfigurationError, "Method #{method_name} already defined"
|
31
|
+
when :force
|
32
|
+
begin
|
33
|
+
target.send(:remove_method, method_name)
|
34
|
+
rescue
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
block.call
|
38
|
+
else
|
39
|
+
raise ConfigurationError, "Unknown collision policy: #{collision}"
|
40
|
+
end
|
41
|
+
else
|
42
|
+
block.call
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/expectant/version.rb
CHANGED
data/lib/expectant.rb
CHANGED
@@ -1,14 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "expectant/version"
|
4
|
+
require_relative "expectant/errors"
|
5
|
+
require_relative "expectant/configuration"
|
6
|
+
require_relative "expectant/types"
|
4
7
|
require_relative "expectant/dsl"
|
5
|
-
require_relative "expectant/schema_builder"
|
6
8
|
require_relative "expectant/expectation"
|
7
9
|
|
8
10
|
module Expectant
|
9
|
-
class
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
class << self
|
12
|
+
attr_writer :configuration
|
13
|
+
|
14
|
+
def configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield(configuration)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset_configuration!
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
13
25
|
end
|
14
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expectant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Hector Medina Fetterman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-10-
|
11
|
+
date: 2025-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-validation
|
@@ -24,7 +24,38 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.10'
|
27
|
-
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.0'
|
55
|
+
description: Expectant provides a clean DSL for defining multiple validation schemas
|
56
|
+
in a single class. Built on dry-validation and dry-types, it supports custom rules,
|
57
|
+
defaults, fallbacks, and context-aware validations, making it easy to validate inputs,
|
58
|
+
outputs, and any structured data in your Ruby applications.
|
28
59
|
email:
|
29
60
|
- javi@digitalhospital.com
|
30
61
|
executables: []
|
@@ -34,16 +65,19 @@ files:
|
|
34
65
|
- ".rspec"
|
35
66
|
- ".standard.yml"
|
36
67
|
- CHANGELOG.md
|
37
|
-
- CODE_OF_CONDUCT.md
|
38
68
|
- LICENSE.txt
|
39
69
|
- README.md
|
40
70
|
- Rakefile
|
41
71
|
- lib/expectant.rb
|
72
|
+
- lib/expectant/bound_schema.rb
|
73
|
+
- lib/expectant/configuration.rb
|
42
74
|
- lib/expectant/dsl.rb
|
75
|
+
- lib/expectant/errors.rb
|
43
76
|
- lib/expectant/expectation.rb
|
44
|
-
- lib/expectant/
|
77
|
+
- lib/expectant/schema.rb
|
78
|
+
- lib/expectant/types.rb
|
79
|
+
- lib/expectant/utils.rb
|
45
80
|
- lib/expectant/version.rb
|
46
|
-
- sig/expectant.rbs
|
47
81
|
homepage: https://github.com/DigitalHospital/expectant
|
48
82
|
licenses:
|
49
83
|
- MIT
|
@@ -69,5 +103,5 @@ requirements: []
|
|
69
103
|
rubygems_version: 3.4.10
|
70
104
|
signing_key:
|
71
105
|
specification_version: 4
|
72
|
-
summary:
|
106
|
+
summary: A flexible DSL for defining reusable validation schemas
|
73
107
|
test_files: []
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,132 +0,0 @@
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
2
|
-
|
3
|
-
## Our Pledge
|
4
|
-
|
5
|
-
We as members, contributors, and leaders pledge to make participation in our
|
6
|
-
community a harassment-free experience for everyone, regardless of age, body
|
7
|
-
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
-
identity and expression, level of experience, education, socio-economic status,
|
9
|
-
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
-
identity and orientation.
|
11
|
-
|
12
|
-
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
-
diverse, inclusive, and healthy community.
|
14
|
-
|
15
|
-
## Our Standards
|
16
|
-
|
17
|
-
Examples of behavior that contributes to a positive environment for our
|
18
|
-
community include:
|
19
|
-
|
20
|
-
* Demonstrating empathy and kindness toward other people
|
21
|
-
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
-
* Giving and gracefully accepting constructive feedback
|
23
|
-
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
-
and learning from the experience
|
25
|
-
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
-
community
|
27
|
-
|
28
|
-
Examples of unacceptable behavior include:
|
29
|
-
|
30
|
-
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
-
any kind
|
32
|
-
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
-
* Public or private harassment
|
34
|
-
* Publishing others' private information, such as a physical or email address,
|
35
|
-
without their explicit permission
|
36
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
-
professional setting
|
38
|
-
|
39
|
-
## Enforcement Responsibilities
|
40
|
-
|
41
|
-
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
-
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
-
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
-
or harmful.
|
45
|
-
|
46
|
-
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
-
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
-
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
-
decisions when appropriate.
|
50
|
-
|
51
|
-
## Scope
|
52
|
-
|
53
|
-
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
-
an individual is officially representing the community in public spaces.
|
55
|
-
Examples of representing our community include using an official email address,
|
56
|
-
posting via an official social media account, or acting as an appointed
|
57
|
-
representative at an online or offline event.
|
58
|
-
|
59
|
-
## Enforcement
|
60
|
-
|
61
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
-
reported to the community leaders responsible for enforcement at
|
63
|
-
[INSERT CONTACT METHOD].
|
64
|
-
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
-
|
66
|
-
All community leaders are obligated to respect the privacy and security of the
|
67
|
-
reporter of any incident.
|
68
|
-
|
69
|
-
## Enforcement Guidelines
|
70
|
-
|
71
|
-
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
-
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
-
|
74
|
-
### 1. Correction
|
75
|
-
|
76
|
-
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
-
unprofessional or unwelcome in the community.
|
78
|
-
|
79
|
-
**Consequence**: A private, written warning from community leaders, providing
|
80
|
-
clarity around the nature of the violation and an explanation of why the
|
81
|
-
behavior was inappropriate. A public apology may be requested.
|
82
|
-
|
83
|
-
### 2. Warning
|
84
|
-
|
85
|
-
**Community Impact**: A violation through a single incident or series of
|
86
|
-
actions.
|
87
|
-
|
88
|
-
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
-
interaction with the people involved, including unsolicited interaction with
|
90
|
-
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
-
includes avoiding interactions in community spaces as well as external channels
|
92
|
-
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
-
ban.
|
94
|
-
|
95
|
-
### 3. Temporary Ban
|
96
|
-
|
97
|
-
**Community Impact**: A serious violation of community standards, including
|
98
|
-
sustained inappropriate behavior.
|
99
|
-
|
100
|
-
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
-
communication with the community for a specified period of time. No public or
|
102
|
-
private interaction with the people involved, including unsolicited interaction
|
103
|
-
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
-
Violating these terms may lead to a permanent ban.
|
105
|
-
|
106
|
-
### 4. Permanent Ban
|
107
|
-
|
108
|
-
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
-
standards, including sustained inappropriate behavior, harassment of an
|
110
|
-
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
-
|
112
|
-
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
-
community.
|
114
|
-
|
115
|
-
## Attribution
|
116
|
-
|
117
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
-
version 2.1, available at
|
119
|
-
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
-
|
121
|
-
Community Impact Guidelines were inspired by
|
122
|
-
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
-
|
124
|
-
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
-
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
-
[https://www.contributor-covenant.org/translations][translations].
|
127
|
-
|
128
|
-
[homepage]: https://www.contributor-covenant.org
|
129
|
-
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
-
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
-
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
-
[translations]: https://www.contributor-covenant.org/translations
|
@@ -1,86 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/validation'
|
4
|
-
|
5
|
-
module Expectant
|
6
|
-
class SchemaBuilder
|
7
|
-
def initialize(schema_definition)
|
8
|
-
@fields = schema_definition[:fields]
|
9
|
-
@rules = schema_definition[:rules]
|
10
|
-
end
|
11
|
-
|
12
|
-
def build
|
13
|
-
fields = @fields
|
14
|
-
rules = @rules
|
15
|
-
|
16
|
-
Dry::Validation.Contract do
|
17
|
-
# Define schema based on fields
|
18
|
-
params do
|
19
|
-
fields.each do |field|
|
20
|
-
if field.required?
|
21
|
-
case field.dry_validation_type
|
22
|
-
when :integer
|
23
|
-
required(field.name).value(:integer)
|
24
|
-
when :float
|
25
|
-
required(field.name).value(:float)
|
26
|
-
when :string
|
27
|
-
required(field.name).value(:string)
|
28
|
-
when :bool
|
29
|
-
required(field.name).value(:bool)
|
30
|
-
when :array
|
31
|
-
required(field.name).value(:array)
|
32
|
-
when :hash
|
33
|
-
required(field.name).value(:hash)
|
34
|
-
else
|
35
|
-
required(field.name).filled
|
36
|
-
end
|
37
|
-
else
|
38
|
-
case field.dry_validation_type
|
39
|
-
when :integer
|
40
|
-
optional(field.name).maybe(:integer)
|
41
|
-
when :float
|
42
|
-
optional(field.name).maybe(:float)
|
43
|
-
when :string
|
44
|
-
optional(field.name).maybe(:string)
|
45
|
-
when :bool
|
46
|
-
optional(field.name).maybe(:bool)
|
47
|
-
when :array
|
48
|
-
optional(field.name).maybe(:array)
|
49
|
-
when :hash
|
50
|
-
optional(field.name).maybe(:hash)
|
51
|
-
else
|
52
|
-
optional(field.name).value(:any)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# Add custom rules
|
59
|
-
rules.each do |rule_def|
|
60
|
-
if rule_def[:name]
|
61
|
-
# Single key or multiple keys
|
62
|
-
if rule_def[:name].is_a?(Array)
|
63
|
-
rule(*rule_def[:name], &rule_def[:block])
|
64
|
-
else
|
65
|
-
rule(rule_def[:name], &rule_def[:block])
|
66
|
-
end
|
67
|
-
else
|
68
|
-
# Global rule without a specific field
|
69
|
-
rule(&rule_def[:block])
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
# Add type validation for custom classes
|
74
|
-
fields.each do |field|
|
75
|
-
next unless field.type.is_a?(Class)
|
76
|
-
|
77
|
-
rule(field.name) do
|
78
|
-
if value && !value.is_a?(field.type)
|
79
|
-
key.failure("must be an instance of #{field.type}")
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
data/sig/expectant.rbs
DELETED