MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / jsonschema_for_fields

Function jsonschema_for_fields

etc/scripts/sch2js/sch2js.py:92–138  ·  view source on GitHub ↗

Return a mapping for the schema of a collection of fields.

(model)

Source from the content-addressed store, hash-verified

90 return field_schema
91
92 def jsonschema_for_fields(model):
93 """
94 Return a mapping for the schema of a collection of fields.
95 """
96 properties = {}
97 required = []
98 for field_name, field_instance in model.fields.items():
99 serialized_name = getattr(field_instance, "serialized_name", None) or field_name
100
101 if isinstance(field_instance, ModelType):
102 node = jsonschema_for_model(field_instance.model_class)
103
104 elif isinstance(field_instance, ListType):
105 try:
106 node = jsonschema_for_model(field_instance.model_class, "array")
107 if hasattr(field_instance, "metadata"):
108 _node = {}
109 _node["type"] = node.pop("type")
110 _node["title"] = field_instance.metadata.get("label", "")
111 _node["description"] = field_instance.metadata.get("description", "")
112 _node.update(node)
113 node = _node
114 except AttributeError:
115 field_schema = jsonschema_for_single_field(field_instance.field)
116 node = {}
117 node["type"] = "array"
118 if hasattr(field_instance, "metadata"):
119 node["title"] = field_instance.metadata.get("label", "")
120 node["description"] = field_instance.metadata.get("description", "")
121 node["items"] = field_schema
122
123 # Convert field as single model
124 elif isinstance(field_instance, BaseType):
125 node = jsonschema_for_single_field(field_instance)
126
127 if getattr(field_instance, "required", False):
128 required.append(serialized_name)
129 properties[serialized_name] = node
130 else:
131 properties[serialized_name] = {
132 "oneOf": [
133 node,
134 {"type": "null"},
135 ]
136 }
137
138 return properties, required
139
140 def jsonschema_for_model(model, _type="object"):
141 """

Callers 1

jsonschema_for_modelFunction · 0.85

Calls 6

jsonschema_for_modelFunction · 0.85
getMethod · 0.65
popMethod · 0.45
updateMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected