40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/circuitdata/compatibility_checker.rb', line 40
def perform_comparison(product_data, check_data, schema, type)
docu = Circuitdata::Tools.new()
ra = docu.create_structure
case type
when 'restricted'
check_hash = check_data.dig(:open_trade_transfer_package, :profiles, :restricted, :printed_circuits_fabrication_data)
when 'enforced'
check_hash = check_data.dig(:open_trade_transfer_package, :profiles, :enforced, :printed_circuits_fabrication_data)
when 'capabilities'
check_hash = check_data.dig(:open_trade_transfer_package, :capabilities, :printed_circuits_fabrication_data)
else
check_hash = {}
end
common_hash = schema.dig(:properties, :open_trade_transfer_package, :properties, :products, :patternProperties, :'^(?!generic$).*', :properties, :printed_circuits_fabrication_data, :properties)
check_hash.each do |k, v|
v.each do |kl1, vl1| unless [:dielectric, :soldermask, :stiffener].include? k.to_sym
common_hash[k.to_sym]||= {:type => 'object', :properties => {}}
end
enum = []
required = []
case type
when 'restricted'
case vl1[0].class.name
when 'String'
vl1.each {|enumvalue| enum << enumvalue.strip}
new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
when 'Boolean', 'FalseClass', 'TrueClass'
vl1.each do |enumvalue|
required << kl1.to_s if enumvalue == false
enum << enumvalue
end
new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
when 'Numeric', 'Float'
if ra.dig(:structured, :elements, k.to_sym, :elements, kl1.to_sym, :enum).nil?
new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
else
vl1.each {|enumvalue| enum << enumvalue}
new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
end
end
when 'enforced'
enum << vl1
new_hash = eval("{:enum => #{enum}}")
when 'capabilities'
case vl1[0].class.name
when 'String'
vl1.each {|enumvalue| enum << enumvalue.strip}
new_hash = {:anyOf => [{ :enum => enum }]}
when 'Boolean', 'FalseClass', 'TrueClass'
vl1.each do |enumvalue|
required << kl1.to_s if enumvalue == true
enum << enumvalue
end
new_hash = {:anyOf => [{ :enum => enum }]}
when 'Numeric'
vl1.each {|enumvalue| enum << enumvalue.strip}
new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
when 'Integer', 'Float'
if vl1.length == 1
new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[0] }]}
elsif vl1.length == 2
new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
else
fail StandardError, "Unhandled array length=#{vl1.length} key=#{kl1}"
end
else
fail StandardError, "Unhandled type=#{vl1[1].class.name} key=#{kl1}"
end
end
case k.to_sym
when :dielectric
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:dielectrics][:items][:properties][kl1.to_sym] = new_hash
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:dielectrics][:items][:required] = required
when :soldermask
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:soldermasks][:items][:properties][kl1.to_sym] = new_hash
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:soldermasks][:items][:required] = required
when :stiffener
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:stiffeners][:items][:properties][kl1.to_sym] = new_hash
schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:stiffeners][:items][:required] = required
else
common_hash[k.to_sym][:properties][kl1.to_sym] = new_hash
common_hash[k.to_sym][:required] = required
end
end if v.is_a? Hash
common_hash[k.to_sym][:additionalProperties] = false if (v.is_a?(Hash) && type == 'capabilities')
end
begin
validation_errors = JSON::Validator.fully_validate(schema.to_json, product_data, :errors_as_objects => true)
if validation_errors.any?
@fh[:error] = true
@fh[:message] = 'The product to check did not meet the requirements'
validation_errors.each do |error|
error_array = []
begin
error_array << error[:message].match("^(The\\sproperty\\s\\'[\\s\\S]*\\'\\s)([\\s\\S]*)(\\sin\\sschema[\\s\\S]*)$").captures[1]
rescue
error_array << error[:message]
end
@fh[:errors][type.to_sym][error[:fragment]] = error_array
end
end
rescue JSON::Schema::ReadFailed
@fh[:error] = true
@fh[:message] = "Could not read the submitted `#{type}` schema" rescue JSON::Schema::SchemaError
@fh[:error] = true
@fh[:message] = "Something was wrong with the submitted `#{type}` schema" end
end
|