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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0f853475790323284d4fe5b10a7cacf89dc188d8475cbc8b71362f9a91f29b9
4
- data.tar.gz: 9e02cc197ee1e5c2a544c9523d2d07cf4f8c7e0ff33864853a13acfd8d486e90
3
+ metadata.gz: 17d51cc1b8a68838a9778650d99d537f259266be3236a7e40a3c31c4010070ca
4
+ data.tar.gz: 410d6a41f2afc28ca7acbe47aae6af0b8e650703e352cee27b49849713662d9c
5
5
  SHA512:
6
- metadata.gz: ca640d8347b57db0df371ae7138167930f9368cedbff5669f6e05e180f07a1a84f478d4e1d76c123aa41718b5ed92a6ae7fd9777968185a07c50a7d904f5350c
7
- data.tar.gz: b561b6fbfde7f97c07af05c918596f61fcb148f5ed569a7e6304a64753ad850d1a770d0b570e2cb7e9637d19bcaccf082a43235334f5fefcb2d47414f9510fad
6
+ metadata.gz: 8e6fc90236c2bb6e1228cd21bd70da0c1ca7a16e9c16649c9071ce43abe540d4ebf68bd7e5e3b5a7195a10fe177918947626e5b64c9d11b498bc4011a4a6495f
7
+ data.tar.gz: 9bf263074fb1f71e679fb24a8d4438282be845b34eece40ec1179b8c5cc0c3052b075ae39ed917f4026fe6f3b477d0ada01bef7980d5469ca7fbd6938b45077d
@@ -1,40 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sukima
4
- class Constraints
5
- @registrations = {}
6
-
4
+ module Constraints
7
5
  class << self
8
- attr_reader :registrations
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
- 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
- class Format
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
- class Length
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
- class Nonnil
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, *args, **, &)
11
- schema = args.first.is_a?(Sukima) ? args.shift : Sukima.new(*args, **, &)
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(*args, **, &)
21
- schema = args.first.is_a?(Sukima) ? args.shift : Sukima.new(*args, **, &)
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?
@@ -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 if @errors.any?
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.dup.map! { "#{prefix}#{_1}" }
34
- @children.map do |name, error_field|
35
- result.concat(error_field.messages([*path, name]))
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.registrations[key].call(args, value)
20
+ message = Constraints.send(key, args, value)
21
21
  error_field.errors << message if message
22
22
  end
23
- Dsl.new(value, error_field).instance_exec(value, &@block) if @block
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.1
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-27 00:00:00.000000000 Z
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 in only
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: []