rsmp_schema 0.2.0 → 0.2.3

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: badefc8cfd4f1df332cad770cd02b9394d19fe327860206cf59ed745b7f051cd
4
- data.tar.gz: d04eebeede6747cd5b245ef66c83b1d0256c14bd7df21f6f2d94176bfd146536
3
+ metadata.gz: e82b6ea883b37ce64d134d66ba9980891dd9ecf8f1f7fc12df75df212b77d7a1
4
+ data.tar.gz: 242013a26a727cb7ec5281c3e3d60f68e2799999238503ad2701e209fb7112ec
5
5
  SHA512:
6
- metadata.gz: 4ba94206d0fc23153dff5474dc4fea59112d24319a7bf1932ea5ada2bf1f77412b2231913cce74f11f69f9783fa319aff41b0a57cc08a35770ee401e7c01205f
7
- data.tar.gz: afb8edf5054d2e430b174bb881b149eab1487891ef774239e3cbc9b87419b1afbbc2aa7a6504e35a512c7694480c762c08e42a77c72a84fa673e2d3c3ed9db63
6
+ metadata.gz: '046758412e06b7b8ddea3ecb050cd0d0d32d55f901f85b74a2cd1032def20417d7dacdeea9710234730ef2b03b6e29152f230be6ac6eda295154beeaa925b2a5'
7
+ data.tar.gz: f27d0648c9023b301caca1a5d311ef02577afe6c8adf54ca07c2ba3dc3b8f71b114dd451a7464e61a93be5d560b6b4a2e07b2c02df15392a60fa6a4d2c777b54
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rsmp_schema (0.2.0)
4
+ rsmp_schema (0.2.3)
5
5
  json_schemer (~> 0.2.21)
6
6
  thor (~> 1.2.1)
7
7
 
@@ -108,13 +108,19 @@ module RSMP
108
108
  # convert yaml values to jsons schema enum
109
109
  def self.handle_enum item, out
110
110
  if item["values"]
111
- case item["values"]
111
+ out["enum"] = case item["values"]
112
112
  when Hash
113
- out["enum"] = item["values"].keys.sort
113
+ item["values"].keys.sort
114
114
  when Array
115
- out["enum"] = item["values"].sort
115
+ item["values"].sort
116
116
  else
117
- raise "Error: Values must be specified as either a Hash or an Array"
117
+ raise "Error: Values must be specified as either a Hash or an Array, got #{item["values"].class}"
118
+ end.map do |v|
119
+ if v.is_a?(Integer) || v.is_a?(Float)
120
+ v.to_s
121
+ else
122
+ v
123
+ end
118
124
  end
119
125
  end
120
126
  end
@@ -6,32 +6,66 @@ end
6
6
  module RSMP::Schema
7
7
  @@schemas = nil
8
8
 
9
- def self.schemas
10
- return @@schemas if @@schemas
9
+ def self.setup
10
+ @@schemas = {}
11
11
  schemas_path = File.expand_path( File.join(__dir__,'..','..','schemas') )
12
- @@schemas = {
13
- core: {
14
- '3.1.1' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.1','rsmp.json')) ),
15
- '3.1.2' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.2','rsmp.json')) ),
16
- '3.1.3' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.3','rsmp.json')) ),
17
- '3.1.4' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.4','rsmp.json')) ),
18
- '3.1.5' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.5','rsmp.json')) ),
19
- '3.2' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.2', 'rsmp.json')) )
20
- },
21
- # note that tlc 1.0.11 and 1.0.12 does not exist (unreleased drafts)
22
- tlc: {
23
- '1.0.7' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.7' ,'sxl.json')) ),
24
- '1.0.8' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.8' ,'sxl.json')) ),
25
- '1.0.9' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.9' ,'sxl.json')) ),
26
- '1.0.10' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.10','sxl.json')) ),
27
- '1.0.13' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.13','sxl.json')) ),
28
- '1.0.14' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.14','sxl.json')) ),
29
- '1.0.15' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.15','sxl.json')) ),
30
- '1.1' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.1', 'sxl.json')) )
31
- }
32
- }
12
+ Dir.glob("#{schemas_path}/*").select {|f| File.directory? f}.each do |type_path|
13
+ type = File.basename(type_path).to_sym
14
+ @@schemas[type] = {}
15
+ Dir.glob("#{type_path}/*").select {|f| File.directory? f}.each do |schema_path|
16
+ version = File.basename(schema_path)
17
+ if type == :core
18
+ file = 'rsmp.json'
19
+ else
20
+ file = 'sxl.json'
21
+ end
22
+ @@schemas[type][version] = JSONSchemer.schema(
23
+ Pathname.new(File.join(schema_path,file))
24
+ )
25
+ end
26
+ end
27
+ end
28
+
29
+ # get all schemas, oganized by type and version
30
+ def self.schemas
31
+ raise RuntimeError.new("No schemas available, perhaps Schema.setup was never called?") unless @@schemas
32
+ @@schemas
33
+ end
34
+
35
+ # get array of core schema versions
36
+ def self.core_versions
37
+ versions :core
38
+ end
39
+
40
+ # get earliest core schema version
41
+ def self.earliest_core_version
42
+ earliest_version :core
33
43
  end
34
44
 
45
+ # get latesty core schema version
46
+ def self.latest_core_version
47
+ latest_version :core
48
+ end
49
+
50
+ # get array of schema versions for a particular schema type
51
+ def self.versions type
52
+ schemas = find_schemas!(type).keys
53
+ sort_versions(schemas)
54
+ end
55
+
56
+ # get earliest schema version for a particular schema type
57
+ def self.earliest_version type
58
+ schemas = find_schemas!(type).keys
59
+ sort_versions(schemas).first
60
+ end
61
+
62
+ # get latest schema version for a particular schema type
63
+ def self.latest_version type
64
+ schemas = find_schemas!(type).keys
65
+ sort_versions(schemas).last
66
+ end
67
+
68
+ # validate an rsmp messages using a schema object
35
69
  def self.validate_using_schema message, schema
36
70
  raise ArgumentError.new("message missing") unless message
37
71
  raise ArgumentError.new("schema missing") unless schema
@@ -44,17 +78,28 @@ module RSMP::Schema
44
78
  end
45
79
  end
46
80
 
81
+ # sort version strings
82
+ def self.sort_versions versions
83
+ versions.sort_by { |k| Gem::Version.new(k) }
84
+ end
85
+
86
+ # find schemas versions for particular schema type
87
+ # return nil if type not found
47
88
  def self.find_schemas type
48
89
  raise ArgumentError.new("type missing") unless type
49
- schemas[type.to_sym]
90
+ schemas = @@schemas[type.to_sym]
50
91
  end
51
92
 
93
+ # find schemas versions for particular schema type
94
+ # raise error if not found
52
95
  def self.find_schemas! type
53
96
  schemas = find_schemas type
54
97
  raise UnknownSchemaTypeError.new("Unknown schema type #{type}") unless schemas
55
98
  schemas
56
99
  end
57
100
 
101
+ # find schema for a particular schema and version
102
+ # return nil if not found
58
103
  def self.find_schema type, version, options={}
59
104
  raise ArgumentError.new("version missing") unless version
60
105
  version = sanitize_version version if options[:lenient]
@@ -65,11 +110,18 @@ module RSMP::Schema
65
110
  nil
66
111
  end
67
112
 
113
+ # get major.minor.patch part of a version string, where patch is optional
114
+ # ignore trailing characters, e.g.
115
+ # 3.1.3.32A => 3.1.3
116
+ # 3.1A3r3 >= 3.1
117
+ # return nil if string doesn't match
68
118
  def self.sanitize_version version
69
119
  matched = /^\d+\.\d+(\.\d+)?/.match version
70
120
  matched.to_s if matched
71
121
  end
72
122
 
123
+ # find schema for a particular schema and version
124
+ # raise error if not found
73
125
  def self.find_schema! type, version, options={}
74
126
  schema = find_schema type, version, options
75
127
  raise ArgumentError.new("version missing") unless version
@@ -82,10 +134,14 @@ module RSMP::Schema
82
134
  raise UnknownSchemaVersionError.new("Unknown schema version #{type} #{version}")
83
135
  end
84
136
 
137
+ # true if a particular schema type and version found
85
138
  def self.has_schema? type, version, options={}
86
139
  find_schema(type,version, options) != nil
87
140
  end
88
141
 
142
+ # validate using a particular schema and version
143
+ # raises error if schema is not found
144
+ # return nil if validation succeds, otherwise returns an array of errors
89
145
  def self.validate message, schemas, options={}
90
146
  raise ArgumentError.new("message missing") unless message
91
147
  raise ArgumentError.new("schemas missing") unless schemas
@@ -1,5 +1,5 @@
1
1
  module RSMP
2
2
  module Schema
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
data/lib/rsmp_schema.rb CHANGED
@@ -5,3 +5,5 @@ require 'rsmp_schema/schema'
5
5
  require 'rsmp_schema/convert/import/yaml'
6
6
  require 'rsmp_schema/convert/export/json_schema'
7
7
  require 'rsmp_schema/version'
8
+
9
+ RSMP::Schema.setup
@@ -26,11 +26,10 @@
26
26
  "s" : {
27
27
  "allOf" : [
28
28
  {
29
- "description" : "0: Nobody logged in \n1: Operator logged in at level 1 (read only) \n2: Operator logged in at level 2 (read/write)",
30
29
  "enum" : [
31
- 0,
32
- 1,
33
- 2
30
+ "0",
31
+ "1",
32
+ "2"
34
33
  ]
35
34
  },
36
35
  {
@@ -26,11 +26,10 @@
26
26
  "s" : {
27
27
  "allOf" : [
28
28
  {
29
- "description" : "0: Nobody logged in \n1: Operator logged in at level 1 (read only) \n2: Operator logged in at level 2 (read/write)",
30
29
  "enum" : [
31
- 0,
32
- 1,
33
- 2
30
+ "0",
31
+ "1",
32
+ "2"
34
33
  ]
35
34
  },
36
35
  {
@@ -781,8 +781,7 @@ objects:
781
781
  arguments:
782
782
  user:
783
783
  type: integer
784
- values: [0,1,2]
785
- description: |-
784
+ values:
786
785
  0: Nobody logged in
787
786
  1: Operator logged in at level 1 (read only)
788
787
  2: Operator logged in at level 2 (read/write)
@@ -793,8 +792,7 @@ objects:
793
792
  arguments:
794
793
  user:
795
794
  type: integer
796
- values: [0,1,2]
797
- description: |-
795
+ values:
798
796
  0: Nobody logged in
799
797
  1: Operator logged in at level 1 (read only)
800
798
  2: Operator logged in at level 2 (read/write)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsmp_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Tin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-24 00:00:00.000000000 Z
11
+ date: 2022-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_schemer