sukima 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sukima/constraints.rb +7 -28
- data/lib/sukima/dsl.rb +4 -4
- data/lib/sukima/error_field.rb +4 -8
- data/lib/sukima.rb +5 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3a1f4531a794d22f40e97a2040a3ceb06c81071a7ed2313c13fa2eec8a6a66b
|
4
|
+
data.tar.gz: d0f61856e6a1b96f90a0e31d48365eb8ee93da9bda90be604d98404741438a4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7995b0ac51c9af4183e7430969d8274d5e31ab181eb911efc36aa2fb072a77ad9156b9b80eee46fb7d55bf75cdc54ee39aac372688ab7d683afed1ef0a686ac9
|
7
|
+
data.tar.gz: a2fe41957351e0e1ee95b31a8de3343d8df2db2108db7a54f74f46815287a297dfabb7a060cafd222da562ad4aa21c1e06c7f88968a3fcbf39779d2754eb5811
|
data/lib/sukima/constraints.rb
CHANGED
@@ -1,40 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Sukima
|
4
|
-
|
5
|
-
@registrations = {}
|
6
|
-
|
4
|
+
module Constraints
|
7
5
|
class << self
|
8
|
-
|
9
|
-
|
10
|
-
def const_added(const_name)
|
11
|
-
constrant_name = const_name.to_s
|
12
|
-
constrant_name.gsub!(/(?<=[[:lower:]])(?=[[:upper:]])/, '_')
|
13
|
-
constrant_name.downcase!
|
14
|
-
@registrations[constrant_name.to_sym] = const_get(const_name).new
|
6
|
+
def type(type, value)
|
7
|
+
"should be #{type}" unless value.is_a?(type)
|
15
8
|
end
|
16
|
-
end
|
17
9
|
|
18
|
-
|
19
|
-
def call(type, value)
|
20
|
-
"should be #{type}" unless value.nil? || value.is_a?(type)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class In
|
25
|
-
def call(range, value)
|
10
|
+
def in(range, value)
|
26
11
|
"should be in #{range}" unless range.include?(value)
|
27
12
|
end
|
28
|
-
end
|
29
13
|
|
30
|
-
|
31
|
-
def call(pattern, value)
|
14
|
+
def format(pattern, value)
|
32
15
|
"should match #{pattern.source}" unless value.is_a?(String) && pattern.match?(value)
|
33
16
|
end
|
34
|
-
end
|
35
17
|
|
36
|
-
|
37
|
-
def call(args, value)
|
18
|
+
def length(args, value)
|
38
19
|
case args
|
39
20
|
when Integer
|
40
21
|
"should have length of #{args}" unless value.respond_to?(:length) && value.length == args
|
@@ -42,10 +23,8 @@ class Sukima
|
|
42
23
|
"should have length in #{args}" unless value.respond_to?(:length) && args.include?(value.length)
|
43
24
|
end
|
44
25
|
end
|
45
|
-
end
|
46
26
|
|
47
|
-
|
48
|
-
def call(_, value)
|
27
|
+
def nonnil(_, value)
|
49
28
|
'should not be nil' if value.nil?
|
50
29
|
end
|
51
30
|
end
|
data/lib/sukima/dsl.rb
CHANGED
@@ -7,8 +7,8 @@ class Sukima
|
|
7
7
|
@error_field = error_field
|
8
8
|
end
|
9
9
|
|
10
|
-
def field(name,
|
11
|
-
schema =
|
10
|
+
def field(name, schema = nil, **, &)
|
11
|
+
schema = Sukima.new(**, &) if schema.nil?
|
12
12
|
if @value.key?(name)
|
13
13
|
result = schema.validate(@value[name])
|
14
14
|
@error_field[name] = result unless result.valid?
|
@@ -17,8 +17,8 @@ class Sukima
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def items(
|
21
|
-
schema =
|
20
|
+
def items(schema = nil, **, &)
|
21
|
+
schema = Sukima.new(**, &) if schema.nil?
|
22
22
|
@value.each_with_index do |item, index|
|
23
23
|
result = schema.validate(item)
|
24
24
|
@error_field[index] = result unless result.valid?
|
data/lib/sukima/error_field.rb
CHANGED
@@ -17,12 +17,8 @@ class Sukima
|
|
17
17
|
@children[name] = value
|
18
18
|
end
|
19
19
|
|
20
|
-
def dig(*)
|
21
|
-
@children.dig(*)
|
22
|
-
end
|
23
|
-
|
24
20
|
def valid?
|
25
|
-
return false
|
21
|
+
return false unless @errors.empty?
|
26
22
|
|
27
23
|
@children.each_value { return false unless _1.valid? }
|
28
24
|
true
|
@@ -30,9 +26,9 @@ class Sukima
|
|
30
26
|
|
31
27
|
def messages(path = [])
|
32
28
|
prefix = path.empty? ? '' : "#{path.join('.')} "
|
33
|
-
result = @errors.
|
34
|
-
@children.
|
35
|
-
result.concat(error_field.messages(
|
29
|
+
result = @errors.map { "#{prefix}#{_1}" }
|
30
|
+
@children.each do |name, error_field|
|
31
|
+
result.concat(error_field.messages(path + [name]))
|
36
32
|
end
|
37
33
|
result
|
38
34
|
end
|
data/lib/sukima.rb
CHANGED
@@ -14,13 +14,16 @@ class Sukima
|
|
14
14
|
|
15
15
|
def validate(value)
|
16
16
|
error_field = ErrorField.new
|
17
|
+
return error_field if !@constraints[:nonnil] && value.nil?
|
18
|
+
|
17
19
|
@constraints.each do |key, args|
|
18
20
|
next if key == :required
|
19
21
|
|
20
|
-
message = Constraints.
|
22
|
+
message = Constraints.send(key, args, value)
|
21
23
|
error_field.errors << message if message
|
22
24
|
end
|
23
|
-
|
25
|
+
|
26
|
+
Dsl.new(value, error_field).instance_exec(value, &@block) if @block && value.is_a?(@constraints[:type])
|
24
27
|
error_field
|
25
28
|
end
|
26
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sukima
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weihang Jian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,8 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.64'
|
69
|
-
description: Sukima is a lightweight data validation library for Ruby written
|
70
|
-
100
|
69
|
+
description: Sukima is a lightweight data schema validation library for Ruby written
|
70
|
+
in only ~100 lines of code. It provides a simple and flexible way to define constraints
|
71
71
|
for data and validate it.
|
72
72
|
email: tonytonyjan@gmail.com
|
73
73
|
executables: []
|
@@ -101,5 +101,5 @@ requirements: []
|
|
101
101
|
rubygems_version: 3.5.9
|
102
102
|
signing_key:
|
103
103
|
specification_version: 4
|
104
|
-
summary: Lightweight data validation library for Ruby.
|
104
|
+
summary: Lightweight data schema validation library for Ruby.
|
105
105
|
test_files: []
|