sukima 0.0.1 → 0.0.2
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 +6 -27
- data/lib/sukima/dsl.rb +4 -4
- data/lib/sukima/error_field.rb +4 -8
- data/lib/sukima.rb +3 -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: 17d51cc1b8a68838a9778650d99d537f259266be3236a7e40a3c31c4010070ca
|
4
|
+
data.tar.gz: 410d6a41f2afc28ca7acbe47aae6af0b8e650703e352cee27b49849713662d9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e6fc90236c2bb6e1228cd21bd70da0c1ca7a16e9c16649c9071ce43abe540d4ebf68bd7e5e3b5a7195a10fe177918947626e5b64c9d11b498bc4011a4a6495f
|
7
|
+
data.tar.gz: 9bf263074fb1f71e679fb24a8d4438282be845b34eece40ec1179b8c5cc0c3052b075ae39ed917f4026fe6f3b477d0ada01bef7980d5469ca7fbd6938b45077d
|
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
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class Type
|
19
|
-
def call(type, value)
|
6
|
+
def type(type, value)
|
20
7
|
"should be #{type}" unless value.nil? || value.is_a?(type)
|
21
8
|
end
|
22
|
-
end
|
23
9
|
|
24
|
-
|
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
@@ -17,10 +17,11 @@ class Sukima
|
|
17
17
|
@constraints.each do |key, args|
|
18
18
|
next if key == :required
|
19
19
|
|
20
|
-
message = Constraints.
|
20
|
+
message = Constraints.send(key, args, value)
|
21
21
|
error_field.errors << message if message
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
|
+
Dsl.new(value, error_field).instance_exec(value, &@block) if @block && value.is_a?(@constraints[:type])
|
24
25
|
error_field
|
25
26
|
end
|
26
27
|
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.2
|
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-06-
|
11
|
+
date: 2024-06-30 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+ lines of code. It provides a simple and flexible way to define constraints
|
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: []
|