将对象与json模式进行比较,如果未设置模式默认值,则将其填充
我用它来验证我的一些输入数据,这很好 但是我想更新我的输入数据,以便在未设置模式的值时,它采用模式的默认值将对象与json模式进行比较,如果未设置模式默认值,则将其填充,json,ruby,validation,jsonschema,Json,Ruby,Validation,Jsonschema,我用它来验证我的一些输入数据,这很好 但是我想更新我的输入数据,以便在未设置模式的值时,它采用模式的默认值 input = {'url' => 'http://www.google.com'} schema = { "type": "object", "properties": { "url": { "type": "string", }, "infos":{ "type": "object",
input = {'url' => 'http://www.google.com'}
schema = {
"type": "object",
"properties": {
"url": {
"type": "string",
},
"infos":{
"type": "object",
"properties": {
"content_type": {
"type": "string",
"default": "html",
},
}
}
}
}
#make some magic here to update my input with the schema
output = {
'url' => 'http://www.google.com',
'infos' => {'content_type'=>'html'}
}
知道输入的结构与模式的结构不同,我如何实现这一点
有一个:insert_defaults选项,但我看不到检索填充对象的方法。
似乎与此相关的代码是
我也搜索过谷歌,尝试使用deep_merge(),但由于要比较的结构不完全相同,我不知道如何处理这个问题。以下是我的想法:
def defaultsFromSchema(node,nodekeys=[],datas={})
return unless node.is_a?(Hash)
if node.has_key?('default')
value = node['default']
#create hash and set deep keys
return nodekeys.reverse.inject(value) { |a, n| { n => a } }
else
node.each do |k, v|
keys = nodekeys.clone
if k != 'properties'
keys.push(k)
end
append = defaultsFromSchema(v,keys,datas)
if append
datas = datas.deep_merge(append)
end
end
end
datas
end
这可能吗?是的。你有试过什么吗?我想那只是为了“在验证过程中”。您要么需要找到另一个库,要么为此推出自己的解决方案。是的,我看到了。但这可能是一个起点。