kubernetes_schemas 0.2.0 → 0.3.0

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: d524459f149e5d40587d4e353cbca9488cd6ec29de1b73aacba26c5d2c2d39fc
4
- data.tar.gz: a8f915379e1d60f7894cb1ee24d5e4604442cd56244af595d7d6783a20e7f167
3
+ metadata.gz: 32cac48598bfe734ec4cf7c48e89fb41be913ab2ba8a6c4bb77e8a0aafaeb9f5
4
+ data.tar.gz: 7cd8733adca9ca45178b53209f577c764daf656b37c00afb8750afbebb65f4cc
5
5
  SHA512:
6
- metadata.gz: fd88e8a1d4ed4867312c9feef514b92c9d7becf8b904a081fad248eebcc10850fd35d979b7c48ca1f5c89da535936459ea1dd3ab8dba4db334752b5cfbdbef37
7
- data.tar.gz: 1ca24ab4f7513af62e146b2bb737fcf3fb1d07e24f3b0198f9e941f91ce4e23e35627ea12a9ce55db9258d1dcc02adafb9f13d81d79b28e8543a1e098ed03744
6
+ metadata.gz: 868d384142d016e54117ff09cab5799159f320a2d3b40319ea7337f6a2186011b4a4da6df64ace86ecc5ac78b19a7635ab29c01f2e8b973947a64a6650c3319d
7
+ data.tar.gz: 99f093f1291fbcbbeb199c7ac0f8349964152e4bef2028266b5ec2179aaaa7e9d42d2f3306e571d3fda430bcd158582f91b6c55b6f47790442ed0969614d0aa0
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
25
25
 
26
- spec.add_dependency "json-schema"
26
+ spec.add_dependency "json_schemer"
27
27
 
28
28
  spec.add_development_dependency "bundler", "~> 1.17"
29
29
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,3 +1,3 @@
1
1
  module KubernetesSchemas
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require "kubernetes_schemas/version"
2
- require "json-schema"
2
+ require "json_schemer"
3
3
 
4
4
  module KubernetesSchemas
5
5
  def self.load(kubernetes_version, strict: false)
@@ -7,9 +7,9 @@ module KubernetesSchemas
7
7
  path = File.expand_path("../../schemas/#{kubernetes_version}", __FILE__)
8
8
  Dir.each_child(path).with_object({}) { |filename, hash|
9
9
  definition = JSON.parse(File.read(File.join(path, filename)))
10
- if definition["x-kubernetes-group-version-kind"]
11
- definition["$schema"] = "http://json-schema.org/draft-04/schema#"
12
- definition["x-kubernetes-group-version-kind"].each do |meta|
10
+ if kinds = definition["x-kubernetes-group-version-kind"]
11
+ definition = JSONSchemer.schema(definition)
12
+ kinds.each do |meta|
13
13
  hash["#{meta["group"]}/#{meta["version"]}/#{meta["kind"]}"] = definition
14
14
  end
15
15
  end
@@ -23,12 +23,37 @@ module KubernetesSchemas
23
23
 
24
24
  def validate(resource)
25
25
  if schema = @schemas["#{resource["apiVersion"]}/#{resource["kind"]}"]
26
- validator = JSON::Validator.new(schema, resource, :record_errors => true)
27
- validator.validate
28
- validator.validation_errors.map(&:message)
26
+ schema.validate(resource).map { |e| stringify(e) }
29
27
  else
30
28
  []
31
29
  end
32
30
  end
31
+
32
+ private
33
+ def stringify(error)
34
+ data_pointer, type, schema = error.values_at('data_pointer', 'type', 'schema')
35
+ location = data_pointer.empty? ? 'root' : "property '#{data_pointer}'"
36
+
37
+ case type
38
+ when 'required'
39
+ keys = error.fetch('details').fetch('missing_keys').join(', ')
40
+ "#{location} is missing required keys: #{keys}"
41
+ when 'null', 'string', 'boolean', 'integer', 'number', 'array', 'object'
42
+ "#{location} is not of type: #{type}"
43
+ when 'pattern'
44
+ "#{location} does not match pattern: #{schema.fetch('pattern')}"
45
+ when 'format'
46
+ "#{location} does not match format: #{schema.fetch('format')}"
47
+ when 'const'
48
+ "#{location} is not: #{schema.fetch('const').inspect}"
49
+ when 'enum'
50
+ "#{location} is not one of: #{schema.fetch('enum')}"
51
+ when 'schema'
52
+ "#{location} is present, while it is missing in schema"
53
+ else
54
+ "#{location} is invalid: error_type=#{type}"
55
+ end
56
+ end
57
+
33
58
  end
34
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes_schemas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Forkert
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: json-schema
14
+ name: json_schemer
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="