field_struct-json_schema 1.0.0.pre1
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 +7 -0
- data/CHANGELOG.md +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +225 -0
- data/lib/field_struct/json_schema/builder.rb +195 -0
- data/lib/field_struct/json_schema/cache.rb +51 -0
- data/lib/field_struct/json_schema/copy.rb +38 -0
- data/lib/field_struct/json_schema/defaults.rb +72 -0
- data/lib/field_struct/json_schema/nullable.rb +61 -0
- data/lib/field_struct/json_schema/override.rb +20 -0
- data/lib/field_struct/json_schema/pattern/char_class.rb +203 -0
- data/lib/field_struct/json_schema/pattern/char_set.rb +51 -0
- data/lib/field_struct/json_schema/pattern/escape.rb +61 -0
- data/lib/field_struct/json_schema/pattern/item.rb +72 -0
- data/lib/field_struct/json_schema/pattern/safety/chains.rb +78 -0
- data/lib/field_struct/json_schema/pattern/safety/groups.rb +97 -0
- data/lib/field_struct/json_schema/pattern/safety/paths.rb +126 -0
- data/lib/field_struct/json_schema/pattern/safety.rb +29 -0
- data/lib/field_struct/json_schema/pattern/translator.rb +296 -0
- data/lib/field_struct/json_schema/pattern.rb +31 -0
- data/lib/field_struct/json_schema/scalar.rb +142 -0
- data/lib/field_struct/json_schema/stamp.rb +54 -0
- data/lib/field_struct/json_schema/version.rb +10 -0
- data/lib/field_struct/json_schema.rb +109 -0
- data/sig/field_struct-json_schema.rbs +56 -0
- metadata +89 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'field_struct'
|
|
4
|
+
|
|
5
|
+
unless defined?(FieldStruct::Introspection)
|
|
6
|
+
raise LoadError, 'field_struct-json_schema needs FieldStruct::Introspection, which field_struct >= 1.0.0.pre1 provides; ' \
|
|
7
|
+
'upgrade field_struct'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
require_relative 'json_schema/version'
|
|
11
|
+
require_relative 'json_schema/pattern'
|
|
12
|
+
require_relative 'json_schema/override'
|
|
13
|
+
require_relative 'json_schema/nullable'
|
|
14
|
+
require_relative 'json_schema/defaults'
|
|
15
|
+
require_relative 'json_schema/scalar'
|
|
16
|
+
require_relative 'json_schema/builder'
|
|
17
|
+
require_relative 'json_schema/copy'
|
|
18
|
+
require_relative 'json_schema/stamp'
|
|
19
|
+
require_relative 'json_schema/cache'
|
|
20
|
+
|
|
21
|
+
# The field_struct namespace; this gem adds {JSONSchema}.
|
|
22
|
+
module FieldStruct
|
|
23
|
+
# Generates the JSON Schema (draft 2020-12) of the JSON a FieldStruct class writes with +to_json+.
|
|
24
|
+
module JSONSchema
|
|
25
|
+
# @return [::Hash{::String=>::Object}] the one blank a +:binary+ field counts as missing
|
|
26
|
+
EMPTY_STRING = { 'type' => 'string', 'maxLength' => 0 }.freeze
|
|
27
|
+
# @return [::Hash{::String=>::String}] text that +String#strip+ would reduce to nothing
|
|
28
|
+
BLANK_STRING = { 'type' => 'string', 'pattern' => '^[\x00\t\n\v\f\r ]*$' }.freeze
|
|
29
|
+
# @return [::String] the form +to_json+ gives a BigDecimal
|
|
30
|
+
DECIMAL_PATTERN = '^-?[0-9]+\.[0-9]+$'
|
|
31
|
+
# @return [::String] the dialect named by every generated document
|
|
32
|
+
DRAFT = 'https://json-schema.org/draft/2020-12/schema'
|
|
33
|
+
|
|
34
|
+
# Opt-in: adds +to_json_schema+ to a struct class.
|
|
35
|
+
module Methods
|
|
36
|
+
# @param base [::Class]
|
|
37
|
+
# @return [void]
|
|
38
|
+
def self.included(base)
|
|
39
|
+
base.extend(ClassMethods)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The class-level method +Methods+ adds.
|
|
43
|
+
module ClassMethods
|
|
44
|
+
# @param serialization [::Symbol, ::String] the serialization whose names the schema uses
|
|
45
|
+
# @param freeze [Boolean] true for the shared, deep-frozen schema instead of a fresh copy
|
|
46
|
+
# @return [::Hash{::String=>::Object}] see {FieldStruct::JSONSchema.generate}
|
|
47
|
+
def to_json_schema(serialization: :json, freeze: false)
|
|
48
|
+
FieldStruct::JSONSchema.generate(self, serialization: serialization, freeze: freeze)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
CACHE = Cache.new
|
|
54
|
+
private_constant :CACHE
|
|
55
|
+
|
|
56
|
+
class << self
|
|
57
|
+
# The JSON Schema of the JSON +klass+ writes with +to_json+.
|
|
58
|
+
#
|
|
59
|
+
# @param klass [::Class<FieldStruct::Base>] a +FieldStruct::Base+ subclass
|
|
60
|
+
# @param serialization [::Symbol, ::String] the serialization whose +serialize+ names the properties use
|
|
61
|
+
# @param decimals [::Symbol] +:to_json+ describes decimals as +to_json+ writes them (numbers, unless a field keeps
|
|
62
|
+
# the string); +:string+ describes every decimal as the String +as_json+ gives
|
|
63
|
+
# @param freeze [Boolean] true returns the cached schema itself, deep-frozen, instead of a copy (JS-B44)
|
|
64
|
+
# @return [::Hash{::String=>::Object}] a fresh deep copy (mutating it changes nothing else), or with
|
|
65
|
+
# +freeze: true+ the shared deep-frozen schema
|
|
66
|
+
# @raise [::ArgumentError] on a bad argument, a bad override or a failing hook
|
|
67
|
+
def generate(klass, serialization: :json, decimals: :to_json, freeze: false)
|
|
68
|
+
unless klass.is_a?(::Class) && klass < FieldStruct::Base
|
|
69
|
+
raise ::ArgumentError, "FieldStruct::JSONSchema.generate expects a FieldStruct::Base subclass, got #{klass.inspect}"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
serialization = serialization.to_sym if serialization.is_a?(::String)
|
|
73
|
+
unless serialization.is_a?(::Symbol)
|
|
74
|
+
raise ::ArgumentError, "FieldStruct::JSONSchema.generate expects serialization: to be a Symbol, got #{serialization.inspect}"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
unless %i[to_json string].include?(decimals)
|
|
78
|
+
raise ::ArgumentError, "FieldStruct::JSONSchema.generate: decimals: takes :to_json or :string, got #{decimals.inspect}"
|
|
79
|
+
end
|
|
80
|
+
unless [true, false].include?(freeze)
|
|
81
|
+
raise ::ArgumentError, "FieldStruct::JSONSchema.generate expects freeze: to be true or false, got #{freeze.inspect}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
CACHE.fetch(klass, serialization, decimals, freeze: freeze)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# An ECMA-262 pattern matching exactly the strings +regexp+ matches (under the +u+ flag), or nil.
|
|
88
|
+
#
|
|
89
|
+
# @param regexp [::Regexp]
|
|
90
|
+
# @return [::String, nil] nil when no exact, safe equivalent exists
|
|
91
|
+
def pattern_for(regexp)
|
|
92
|
+
raise ::ArgumentError, "FieldStruct::JSONSchema.pattern_for expects a Regexp, got #{regexp.inspect}" unless regexp.is_a?(::Regexp)
|
|
93
|
+
|
|
94
|
+
Pattern.translate(regexp)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Empties the schema cache (it holds classes alive: call it on code reload).
|
|
98
|
+
#
|
|
99
|
+
# @return [nil]
|
|
100
|
+
def clear_cache
|
|
101
|
+
CACHE.clear
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The field option this adapter reads; registering it lets fields declare it (FS-B10a).
|
|
109
|
+
FieldStruct.register_option(:json_schema, type: [Hash, TrueClass, FalseClass], owner: 'field_struct-json_schema')
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# The field_struct namespace; this gem adds {JSONSchema}.
|
|
2
|
+
module FieldStruct
|
|
3
|
+
# JSON Schema generation for field_struct classes.
|
|
4
|
+
module JSONSchema
|
|
5
|
+
EMPTY_STRING: ::Hash[::String, ::Object]
|
|
6
|
+
BLANK_STRING: ::Hash[::String, ::String]
|
|
7
|
+
DECIMAL_PATTERN: ::String
|
|
8
|
+
DRAFT: ::String
|
|
9
|
+
VERSION: ::String
|
|
10
|
+
|
|
11
|
+
# The JSON Schema of the JSON +klass+ writes with +to_json+.
|
|
12
|
+
#
|
|
13
|
+
# _@param_ `klass` — a +FieldStruct::Base+ subclass
|
|
14
|
+
#
|
|
15
|
+
# _@param_ `serialization` — the serialization whose +serialize+ names the properties use
|
|
16
|
+
#
|
|
17
|
+
# _@param_ `decimals` — +:to_json+ describes decimals as +to_json+ writes them (numbers, unless a field keeps the string); +:string+ describes every decimal as the String +as_json+ gives
|
|
18
|
+
#
|
|
19
|
+
# _@param_ `freeze` — true returns the cached schema itself, deep-frozen, instead of a copy (JS-B44)
|
|
20
|
+
#
|
|
21
|
+
# _@return_ — a fresh deep copy (mutating it changes nothing else), or with
|
|
22
|
+
# +freeze: true+ the shared deep-frozen schema
|
|
23
|
+
def self.generate: (
|
|
24
|
+
singleton(FieldStruct::Base) klass,
|
|
25
|
+
?serialization: (::Symbol | ::String),
|
|
26
|
+
?decimals: ::Symbol,
|
|
27
|
+
?freeze: bool
|
|
28
|
+
) -> ::Hash[::String, ::Object]
|
|
29
|
+
|
|
30
|
+
# An ECMA-262 pattern matching exactly the strings +regexp+ matches (under the +u+ flag), or nil.
|
|
31
|
+
#
|
|
32
|
+
# _@param_ `regexp`
|
|
33
|
+
#
|
|
34
|
+
# _@return_ — nil when no exact, safe equivalent exists
|
|
35
|
+
def self.pattern_for: (::Regexp regexp) -> ::String?
|
|
36
|
+
|
|
37
|
+
# Empties the schema cache (it holds classes alive: call it on code reload).
|
|
38
|
+
def self.clear_cache: () -> void
|
|
39
|
+
|
|
40
|
+
# Opt-in: adds +to_json_schema+ to a struct class.
|
|
41
|
+
module Methods
|
|
42
|
+
# _@param_ `base`
|
|
43
|
+
def self.included: (::Class base) -> void
|
|
44
|
+
|
|
45
|
+
# The class-level method +Methods+ adds.
|
|
46
|
+
module ClassMethods
|
|
47
|
+
# _@param_ `serialization` — the serialization whose names the schema uses
|
|
48
|
+
#
|
|
49
|
+
# _@param_ `freeze` — true for the shared, deep-frozen schema instead of a fresh copy
|
|
50
|
+
#
|
|
51
|
+
# _@return_ — see {FieldStruct::JSONSchema.generate}
|
|
52
|
+
def to_json_schema: (?serialization: (::Symbol | ::String), ?freeze: bool) -> ::Hash[::String, ::Object]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: field_struct-json_schema
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0.pre1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Adrian Madrid
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: field_struct
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 1.0.0.pre1
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '2'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 1.0.0.pre1
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '2'
|
|
32
|
+
description: Generates the JSON Schema of the JSON a FieldStruct class writes with
|
|
33
|
+
to_json and reads with from_json, and translates Ruby Regexps into exact ECMA-262
|
|
34
|
+
patterns.
|
|
35
|
+
email:
|
|
36
|
+
- adrian.madrid@timeworxs.com
|
|
37
|
+
executables: []
|
|
38
|
+
extensions: []
|
|
39
|
+
extra_rdoc_files: []
|
|
40
|
+
files:
|
|
41
|
+
- CHANGELOG.md
|
|
42
|
+
- LICENSE.txt
|
|
43
|
+
- README.md
|
|
44
|
+
- lib/field_struct/json_schema.rb
|
|
45
|
+
- lib/field_struct/json_schema/builder.rb
|
|
46
|
+
- lib/field_struct/json_schema/cache.rb
|
|
47
|
+
- lib/field_struct/json_schema/copy.rb
|
|
48
|
+
- lib/field_struct/json_schema/defaults.rb
|
|
49
|
+
- lib/field_struct/json_schema/nullable.rb
|
|
50
|
+
- lib/field_struct/json_schema/override.rb
|
|
51
|
+
- lib/field_struct/json_schema/pattern.rb
|
|
52
|
+
- lib/field_struct/json_schema/pattern/char_class.rb
|
|
53
|
+
- lib/field_struct/json_schema/pattern/char_set.rb
|
|
54
|
+
- lib/field_struct/json_schema/pattern/escape.rb
|
|
55
|
+
- lib/field_struct/json_schema/pattern/item.rb
|
|
56
|
+
- lib/field_struct/json_schema/pattern/safety.rb
|
|
57
|
+
- lib/field_struct/json_schema/pattern/safety/chains.rb
|
|
58
|
+
- lib/field_struct/json_schema/pattern/safety/groups.rb
|
|
59
|
+
- lib/field_struct/json_schema/pattern/safety/paths.rb
|
|
60
|
+
- lib/field_struct/json_schema/pattern/translator.rb
|
|
61
|
+
- lib/field_struct/json_schema/scalar.rb
|
|
62
|
+
- lib/field_struct/json_schema/stamp.rb
|
|
63
|
+
- lib/field_struct/json_schema/version.rb
|
|
64
|
+
- sig/field_struct-json_schema.rbs
|
|
65
|
+
homepage: https://github.com/gliders-rb/gems
|
|
66
|
+
licenses:
|
|
67
|
+
- MIT
|
|
68
|
+
metadata:
|
|
69
|
+
source_code_uri: https://github.com/gliders-rb/gems/tree/main/adapters/field_struct-json_schema
|
|
70
|
+
changelog_uri: https://github.com/gliders-rb/gems/blob/main/adapters/field_struct-json_schema/CHANGELOG.md
|
|
71
|
+
rubygems_mfa_required: 'true'
|
|
72
|
+
rdoc_options: []
|
|
73
|
+
require_paths:
|
|
74
|
+
- lib
|
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '3.3'
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '0'
|
|
85
|
+
requirements: []
|
|
86
|
+
rubygems_version: 3.6.9
|
|
87
|
+
specification_version: 4
|
|
88
|
+
summary: JSON Schema (draft 2020-12) for field_struct classes.
|
|
89
|
+
test_files: []
|