lib/datacaster/predefined.rb in datacaster-3.0.0 vs lib/datacaster/predefined.rb in datacaster-3.1.0
- old
+ new
@@ -51,10 +51,22 @@
def validate(active_model_validations)
Validator.new(active_model_validations)
end
+ def schema(base)
+ ContextNodes::StructureCleaner.new(base, strategy: :fail)
+ end
+
+ def choosy_schema(base)
+ ContextNodes::StructureCleaner.new(base, strategy: :remove)
+ end
+
+ def partial_schema(base)
+ ContextNodes::StructureCleaner.new(base, strategy: :pass)
+ end
+
# 'Meta' types
def absent(error_key = nil)
error_keys = ['.absent', 'datacaster.errors.absent']
error_keys.unshift(error_key) if error_key
@@ -88,11 +100,29 @@
def pass
transform(&:itself)
end
- def pick(*keys)
+ def switch(*base, **on_clauses)
+ switch =
+ if base.length == 0
+ SwitchNode.new
+ else
+ SwitchNode.new(base)
+ end
+ on_clauses.reduce(switch) do |result, (k, v)|
+ result.on(k, v)
+ end
+ end
+
+ def pass_if(base)
+ ContextNodes::PassIf.new(base)
+ end
+
+ def pick(*keys, strict: false)
+ raise RuntimeError.new("provide keys to pick, e.g. pick(:key)") if keys.empty?
+
must_be(Enumerable) & transform { |value|
result =
keys.map do |key|
if value.respond_to?(:key?) && !value.key?(key)
Datacaster.absent
@@ -207,10 +237,10 @@
if ['true', '1', true].include?(x)
Datacaster.ValidResult(true)
elsif ['false', '0', false].include?(x)
Datacaster.ValidResult(false)
else
- Datacaster.ErrorResult(Datacaster::I18nValues::Key.new(error_keys, value: x))
+ Datacaster.ErrorResult(I18nValues::Key.new(error_keys, value: x))
end
end
end
def to_float(error_key = nil)