|
1 | 1 | from copy import deepcopy
|
| 2 | +from functools import lru_cache |
2 | 3 | import json
|
3 | 4 | from pathlib import Path
|
4 | 5 | from typing import Any, Dict, Iterable, Optional, TypeVar, Union, cast
|
@@ -129,6 +130,14 @@ def _validate_asset_json(data: dict, schema_dir: Union[str, Path]) -> None:
|
129 | 130 | _validate_obj_json(data, schema)
|
130 | 131 |
|
131 | 132 |
|
| 133 | +@lru_cache |
| 134 | +def _get_schema(schema_version: str, schema_name: str) -> Any: |
| 135 | + return requests.get( |
| 136 | + "https://raw.githubusercontent.com/dandi/schema/" |
| 137 | + f"master/releases/{schema_version}/{schema_name}" |
| 138 | + ).json() |
| 139 | + |
| 140 | + |
132 | 141 | def validate(
|
133 | 142 | obj: dict,
|
134 | 143 | schema_version: Optional[str] = None,
|
@@ -184,11 +193,7 @@ def validate(
|
184 | 193 | "Only dandisets and assets can be validated "
|
185 | 194 | "using json schema for older versions"
|
186 | 195 | )
|
187 |
| - schema_filename = schema_map[schema_key] |
188 |
| - schema = requests.get( |
189 |
| - f"https://raw.githubusercontent.com/dandi/schema/" |
190 |
| - f"master/releases/{schema_version}/{schema_filename}" |
191 |
| - ).json() |
| 196 | + schema = _get_schema(schema_version, schema_map[schema_key]) |
192 | 197 | _validate_obj_json(obj, schema, missing_ok)
|
193 | 198 | klass = getattr(models, schema_key)
|
194 | 199 | try:
|
@@ -223,10 +228,7 @@ def migrate(
|
223 | 228 | if version2tuple(schema_version) > version2tuple(to_version):
|
224 | 229 | raise ValueError(f"Cannot migrate from {schema_version} to lower {to_version}.")
|
225 | 230 | if not (skip_validation):
|
226 |
| - schema = requests.get( |
227 |
| - f"https://raw.githubusercontent.com/dandi/schema/" |
228 |
| - f"master/releases/{schema_version}/dandiset.json" |
229 |
| - ).json() |
| 231 | + schema = _get_schema(schema_version, "dandiset.json") |
230 | 232 | _validate_obj_json(obj, schema)
|
231 | 233 | if version2tuple(schema_version) < version2tuple("0.6.0"):
|
232 | 234 | for val in obj.get("about", []):
|
|
0 commit comments