lowtype 1.6.2 → 1.7.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/lib/expressions/type_errors.rb +133 -0
- data/lib/expressions/type_expression.rb +10 -0
- data/lib/lowtype.rb +1 -0
- data/lib/parsers/type_parser.rb +147 -0
- data/lib/types/complex_types.rb +1 -0
- data/lib/types/error_types.rb +1 -0
- data/lib/types/schema.rb +61 -0
- data/lib/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1f10f8ba754648f8989ade0f8341a64c198345005d7563ea5b09604e63d5546b
|
|
4
|
+
data.tar.gz: c48642e88ca4616eef730a2929a756a06f0fe708dc636571ff07ad32ccf563a9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b4057c034994ed7c4d817d44a6e750e96273d01e47bbf5f42453d1452b368e034c02c26f46bd2adde8c14c06362cb4b8e288bd0115eaa936213b2c7c4f2cbf5a
|
|
7
|
+
data.tar.gz: 42bd196d005c75e0e44977e5149d288a57e32ae8286acde5fcb5bdbf752747779e87c0b89aec3f6b328662eb0287e7ad9c07c66051fa21cde10703ed429df591
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Low
|
|
4
|
+
# Checks a JSON/Hash and returns every place a value doesn't match a type.
|
|
5
|
+
module TypeErrors
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# A value in the data and the type it should be. error_nodes returns the ones that don't match.
|
|
9
|
+
SchemaNode = Data.define(
|
|
10
|
+
:path, # A nested path: 'address.postcode' or 'ids[2]'
|
|
11
|
+
:type, # The type the value should be: String | nil
|
|
12
|
+
:value # The value at that path: 42
|
|
13
|
+
) do
|
|
14
|
+
# The type for a message: 'String | nil'
|
|
15
|
+
def expected = TypeErrors.expected(type)
|
|
16
|
+
|
|
17
|
+
# The value for a message: '42'
|
|
18
|
+
def received = TypeErrors.value_received(value)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @returns: Array of SchemaNode objects with errors
|
|
22
|
+
def error_nodes(type:, value:, path: '')
|
|
23
|
+
check(SchemaNode.new(path:, type:, value:))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The node itself when it doesn't match, or the error nodes inside it when it does.
|
|
27
|
+
def check(node)
|
|
28
|
+
return check_expression(node) if node.type.is_a?(TypeExpression)
|
|
29
|
+
return child_nodes(node).flat_map { |child| check(child) } if outer_match?(node)
|
|
30
|
+
|
|
31
|
+
[node]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# 1. Matching any one of the types is enough
|
|
35
|
+
# 2. If none match, the errors are the ones inside the first type the value matches on the outside, like a bad hash value in a Schema.
|
|
36
|
+
# 3. If none match even on the outside, the node itself is the error.
|
|
37
|
+
def check_expression(node)
|
|
38
|
+
return [] if node.value.nil? && node.type.optional?
|
|
39
|
+
|
|
40
|
+
matched = node.type.types.map { |type| node.with(type:) }.select { |option| outer_match?(option) }
|
|
41
|
+
return [node] if matched.empty?
|
|
42
|
+
|
|
43
|
+
results = matched.map { |option| check(option) }
|
|
44
|
+
results.find(&:empty?) || results.first
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A Schema or { K => V } needs a Hash, and [T] an Array. Any other type matches the value as a whole.
|
|
48
|
+
def outer_match?(node)
|
|
49
|
+
node => { type:, value: }
|
|
50
|
+
|
|
51
|
+
case type
|
|
52
|
+
when Types::Schema::Definition, ::Hash then value.is_a?(::Hash)
|
|
53
|
+
when ::Array then value.is_a?(::Array)
|
|
54
|
+
else
|
|
55
|
+
return type.match?(value:) if TypeQuery.complex_type?(expression: type)
|
|
56
|
+
|
|
57
|
+
type.is_a?(Module) && value.is_a?(type)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# A SchemaNode for each value inside a value:
|
|
62
|
+
# Schema[id: T] - Each field, where a missing key is nil
|
|
63
|
+
# [T] - Each element is the same type
|
|
64
|
+
# [X, Y] - Each element at that position is that type
|
|
65
|
+
# { K => V } - Every key and value
|
|
66
|
+
def child_nodes(node)
|
|
67
|
+
node => { path:, type:, value: }
|
|
68
|
+
|
|
69
|
+
case type
|
|
70
|
+
when Types::Schema::Definition
|
|
71
|
+
type.fields.map { |key, field_type| SchemaNode.new(path: join(path, key), type: field_type, value: field(value, key)) }
|
|
72
|
+
when ::Array
|
|
73
|
+
element_types(type, value).each_with_index.map do |element_type, index|
|
|
74
|
+
SchemaNode.new(path: "#{path}[#{index}]", type: element_type, value: value[index])
|
|
75
|
+
end
|
|
76
|
+
when ::Hash
|
|
77
|
+
key_type, value_type = type.first
|
|
78
|
+
value.flat_map do |key, element|
|
|
79
|
+
key_path = join(path, key)
|
|
80
|
+
[SchemaNode.new(path: key_path, type: key_type, value: key), SchemaNode.new(path: key_path, type: value_type, value: element)]
|
|
81
|
+
end
|
|
82
|
+
else []
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def element_types(types, value)
|
|
87
|
+
return Array.new(value.length, types.first) if types.one?
|
|
88
|
+
|
|
89
|
+
types
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Keys can be strings, like parsed JSON's, or symbols.
|
|
93
|
+
def field(value, key)
|
|
94
|
+
return value[key] if value.key?(key)
|
|
95
|
+
return value[key.to_s] if key.is_a?(Symbol)
|
|
96
|
+
|
|
97
|
+
value[key.to_s.to_sym]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def join(path, key)
|
|
101
|
+
return key.to_s if path.empty?
|
|
102
|
+
|
|
103
|
+
"#{path}.#{key}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# A schema's full definition is too long to read in a message, and the value isn't even a Hash.
|
|
107
|
+
def expected(type)
|
|
108
|
+
return 'Hash' if type.is_a?(Types::Schema::Definition)
|
|
109
|
+
|
|
110
|
+
display(type)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def display(type)
|
|
114
|
+
return type.valid_types if type.is_a?(TypeExpression)
|
|
115
|
+
|
|
116
|
+
TypeExpression.new(type:).valid_types
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def value_received(value)
|
|
120
|
+
case value
|
|
121
|
+
when ::Hash then 'Hash'
|
|
122
|
+
when ::Array then 'Array'
|
|
123
|
+
else trim(value.inspect)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def trim(text)
|
|
128
|
+
return text if text.length <= 40
|
|
129
|
+
|
|
130
|
+
"#{text[0, 37]}..."
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'expressions'
|
|
4
4
|
|
|
5
|
+
require_relative 'type_errors'
|
|
5
6
|
require_relative 'value_expression'
|
|
6
7
|
require_relative '../proxies/param_proxy'
|
|
7
8
|
require_relative '../queries/type_query'
|
|
@@ -38,6 +39,10 @@ module Low
|
|
|
38
39
|
@default_value == :LOWTYPE_UNDEFINED
|
|
39
40
|
end
|
|
40
41
|
|
|
42
|
+
def optional?
|
|
43
|
+
!required?
|
|
44
|
+
end
|
|
45
|
+
|
|
41
46
|
# TODO: Might need to accept error class here so that type accessors can be disabled and supply ArgumentError.
|
|
42
47
|
def validate!(value:, proxy:) # rubocop:disable Metrics
|
|
43
48
|
if value.nil?
|
|
@@ -56,6 +61,11 @@ module Low
|
|
|
56
61
|
raise proxy.error_type, e.message, proxy.backtrace(backtrace: e.backtrace, hidden_paths: HIDDEN_PATHS)
|
|
57
62
|
end
|
|
58
63
|
|
|
64
|
+
# SchemaNode objects for every value that doesn't match (does not raise exception).
|
|
65
|
+
def errors(value)
|
|
66
|
+
TypeErrors.error_nodes(type: self, value:)
|
|
67
|
+
end
|
|
68
|
+
|
|
59
69
|
def valid_types
|
|
60
70
|
types = @types.map do |type|
|
|
61
71
|
if type.is_a?(Array)
|
data/lib/lowtype.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative 'adapters/adapter_loader'
|
|
|
6
6
|
require_relative 'definitions/redefiner'
|
|
7
7
|
require_relative 'definitions/type_accessors'
|
|
8
8
|
require_relative 'expressions/expression_helpers'
|
|
9
|
+
require_relative 'parsers/type_parser'
|
|
9
10
|
require_relative 'queries/file_query'
|
|
10
11
|
require_relative 'syntax/syntax'
|
|
11
12
|
require_relative 'types/complex_types'
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'lowkey'
|
|
4
|
+
require 'prism'
|
|
5
|
+
|
|
6
|
+
require_relative '../expressions/type_expression'
|
|
7
|
+
require_relative '../types/complex_types'
|
|
8
|
+
require_relative '../types/error_types'
|
|
9
|
+
|
|
10
|
+
module Low
|
|
11
|
+
# Builds a type expression from text like "String | nil" or "Array[Enum['admin', 'member']]", for types read from files.
|
|
12
|
+
# Ruby parses the text and looks up its constants, but it's never run: only constants, values, "|", "{}" and "[]"
|
|
13
|
+
# on the types below are allowed, since "[]" on other constants can do anything, like Dir['*'] or ENV['KEY'].
|
|
14
|
+
# A value in the union is the default, like "String | 'Hello'", and nil makes the type optional.
|
|
15
|
+
class TypeParser
|
|
16
|
+
# The types allowed before "[]", and how they read what's inside: types, like Array[String], or values, like Enum[:a, :b].
|
|
17
|
+
BRACKETS = { Array => :typed_array, Hash => :typed_hash, Types::Schema => :schema, Types::Enum => :values,
|
|
18
|
+
Types::Status => :values }.freeze
|
|
19
|
+
|
|
20
|
+
# Constants are looked up in the namespace, then in Object. A namespace can hold schemas, like User,
|
|
21
|
+
# and include Low::Types for Boolean, Enum and Schema.
|
|
22
|
+
def self.parse(text, namespace: Types)
|
|
23
|
+
new(text, namespace:).parse
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(text, namespace: Types)
|
|
27
|
+
@text = text
|
|
28
|
+
@namespace = namespace
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def parse
|
|
32
|
+
result = Prism.parse(@text)
|
|
33
|
+
raise_error(result.errors.first.message, result.errors.first.location) if result.errors.any?
|
|
34
|
+
|
|
35
|
+
statements = result.value.statements.body
|
|
36
|
+
raise_error('Expected one type', result.value.location) unless statements.one?
|
|
37
|
+
|
|
38
|
+
expression(statements.first)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# Terms between pipes: types, or values for the default.
|
|
44
|
+
def expression(node)
|
|
45
|
+
types = []
|
|
46
|
+
default_value = :LOWTYPE_UNDEFINED
|
|
47
|
+
|
|
48
|
+
terms(node).each do |term|
|
|
49
|
+
Lowkey::ValueParser.value?(term) ? default_value = value(term) : types << type(term)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
TypeExpression.new(default_value:).tap { |expression| expression.types.concat(types) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def terms(node)
|
|
56
|
+
return terms(node.body.body.first) if node.is_a?(Prism::ParenthesesNode) && node.body&.body&.one?
|
|
57
|
+
return [node] unless call?(node, :|) && node.receiver
|
|
58
|
+
|
|
59
|
+
[node.receiver, *arguments(node)].flat_map { |term| terms(term) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def type(node)
|
|
63
|
+
case node
|
|
64
|
+
when Prism::ConstantReadNode, Prism::ConstantPathNode then constant(node)
|
|
65
|
+
when Prism::HashNode then Types::Schema[fields(node.elements)]
|
|
66
|
+
when Prism::CallNode then brackets(node)
|
|
67
|
+
else raise_error("'#{node.slice}' isn't a type", node.location)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def brackets(node)
|
|
72
|
+
reader = BRACKETS[type(node.receiver)] if call?(node, :[]) && node.receiver
|
|
73
|
+
raise_error("'#{node.slice}' isn't allowed in a type", node.location) unless reader
|
|
74
|
+
|
|
75
|
+
send(reader, node, arguments(node))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def typed_array(_node, arguments)
|
|
79
|
+
arguments.map { |argument| element(argument) }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def typed_hash(node, arguments)
|
|
83
|
+
hash = fields(arguments.flat_map { |argument| pairs(argument) })
|
|
84
|
+
raise_error("Expected 'Hash[K => V]'", node.location) unless hash.one? && hash.keys.first.instance_of?(Class)
|
|
85
|
+
|
|
86
|
+
hash
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def schema(node, arguments)
|
|
90
|
+
fields = fields(arguments.flat_map { |argument| pairs(argument) })
|
|
91
|
+
names = fields.keys.all? { |key| key.is_a?(String) || key.is_a?(Symbol) }
|
|
92
|
+
raise_error('Schema keys are names, not types', node.location) unless fields.any? && names
|
|
93
|
+
|
|
94
|
+
Types::Schema[fields]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def values(node, arguments)
|
|
98
|
+
type(node.receiver)[*arguments.map { |argument| value(argument) }]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def arguments(node)
|
|
102
|
+
node.arguments&.arguments || []
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def pairs(argument)
|
|
106
|
+
argument.is_a?(Prism::KeywordHashNode) ? argument.elements : [argument]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Pairs like "id: Integer", "'id' => Integer" or "String => Integer", where a nested hash is a nested schema.
|
|
110
|
+
def fields(pairs)
|
|
111
|
+
pairs.to_h do |pair|
|
|
112
|
+
raise_error("'#{pair.slice}' isn't a key and type", pair.location) unless pair.is_a?(Prism::AssocNode)
|
|
113
|
+
|
|
114
|
+
[Lowkey::ValueParser.value?(pair.key) ? value(pair.key) : element(pair.key), element(pair.value)]
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# A single required class stays a class, like "Array[String]" does in Ruby. Anything else stays an expression.
|
|
119
|
+
def element(node)
|
|
120
|
+
expression = expression(node)
|
|
121
|
+
single = expression.types.one? && expression.required? && expression.types.first.instance_of?(Class)
|
|
122
|
+
single ? expression.types.first : expression
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def constant(node)
|
|
126
|
+
name = node.slice.delete_prefix('::')
|
|
127
|
+
constant = node.slice.start_with?('::') ? Object.const_get(name) : @namespace.const_get(name)
|
|
128
|
+
raise_error("'#{name}' isn't a type", node.location) unless constant.is_a?(Module) || constant.is_a?(TypeDefinition)
|
|
129
|
+
|
|
130
|
+
constant
|
|
131
|
+
rescue NameError
|
|
132
|
+
raise_error("Unknown type '#{name}'", node.location)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def value(node)
|
|
136
|
+
Lowkey::ValueParser.value(node) { raise_error("'#{node.slice}' isn't a value", node.location) }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def call?(node, name)
|
|
140
|
+
node.is_a?(Prism::CallNode) && node.name == name && node.block.nil?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def raise_error(message, location)
|
|
144
|
+
raise ParseError, "#{message} at #{location.start_offset} in '#{@text}'"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
data/lib/types/complex_types.rb
CHANGED
data/lib/types/error_types.rb
CHANGED
data/lib/types/schema.rb
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'error_types'
|
|
4
|
+
require_relative 'type_definition'
|
|
5
|
+
require_relative '../expressions/type_errors'
|
|
6
|
+
|
|
7
|
+
module Low
|
|
8
|
+
module Types
|
|
9
|
+
# A hash with named keys, each with its own type, like "Schema[id: Integer, email: String | nil]".
|
|
10
|
+
# A key whose type allows nil may be missing. Keys the schema doesn't name are allowed.
|
|
11
|
+
# A nested hash of named keys is a nested schema, like "Schema[address: { city: String }]".
|
|
12
|
+
class Schema
|
|
13
|
+
def self.[](fields)
|
|
14
|
+
Definition.new(fields:)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Definition
|
|
18
|
+
include TypeDefinition
|
|
19
|
+
|
|
20
|
+
attr_reader :fields
|
|
21
|
+
|
|
22
|
+
def initialize(fields:)
|
|
23
|
+
raise AllowedTypeError, 'Schema requires at least one key' if fields.empty?
|
|
24
|
+
|
|
25
|
+
@fields = fields.transform_values { |type| schema?(type) ? Definition.new(fields: type) : type }.freeze
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def match?(value:)
|
|
29
|
+
errors(value).empty?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def errors(value)
|
|
33
|
+
TypeErrors.error_nodes(type: self, value:)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def display_name
|
|
37
|
+
"Schema[#{@fields.map { |key, type| "#{key}: #{TypeErrors.display(type)}" }.join(', ')}]"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def ==(other)
|
|
41
|
+
other.class == self.class && other.fields == @fields
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def eql?(other)
|
|
45
|
+
self == other
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def hash
|
|
49
|
+
[self.class, @fields].hash
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
# A hash with class keys, like "{ String => Integer }", is a typed hash rather than a schema.
|
|
55
|
+
def schema?(type)
|
|
56
|
+
type.is_a?(::Hash) && !type.empty? && type.keys.none? { |key| key.instance_of?(Class) }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lowtype
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
@@ -88,12 +88,14 @@ files:
|
|
|
88
88
|
- lib/definitions/redefiner.rb
|
|
89
89
|
- lib/definitions/type_accessors.rb
|
|
90
90
|
- lib/expressions/expression_helpers.rb
|
|
91
|
+
- lib/expressions/type_errors.rb
|
|
91
92
|
- lib/expressions/type_expression.rb
|
|
92
93
|
- lib/expressions/value_expression.rb
|
|
93
94
|
- lib/factories/type_factory.rb
|
|
94
95
|
- lib/interfaces/adapter_interface.rb
|
|
95
96
|
- lib/interfaces/error_handling.rb
|
|
96
97
|
- lib/lowtype.rb
|
|
98
|
+
- lib/parsers/type_parser.rb
|
|
97
99
|
- lib/proxies/local_proxy.rb
|
|
98
100
|
- lib/proxies/param_proxy.rb
|
|
99
101
|
- lib/proxies/return_proxy.rb
|
|
@@ -106,6 +108,7 @@ files:
|
|
|
106
108
|
- lib/types/complex_types.rb
|
|
107
109
|
- lib/types/enum.rb
|
|
108
110
|
- lib/types/error_types.rb
|
|
111
|
+
- lib/types/schema.rb
|
|
109
112
|
- lib/types/status.rb
|
|
110
113
|
- lib/types/type_definition.rb
|
|
111
114
|
- lib/version.rb
|