Skip to content

Commit

Permalink
Merge pull request #43 from truenas/mrehan/fix-invalid-metadata
Browse files Browse the repository at this point in the history
NAS-130449 / 24.10 / Improve validation for apps
  • Loading branch information
Qubad786 authored Aug 6, 2024
2 parents e3f18cb + e5de889 commit dd5309b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps_ci/scripts/catalog_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_train_data(train_data):
except (json.JSONDecodeError, JsonValidationError) as e:
verrors.add(
'catalog_json',
f'Failed to validate contents of train data ({".".join(list(e.path))}): {e!r}'
f'Failed to validate contents of train data ({".".join([str(p) for p in e.path])}): {e!r}'
)
verrors.check()

Expand Down
27 changes: 27 additions & 0 deletions apps_validation/json_schema_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
APP_ITEM_JSON_SCHEMA = {
'type': 'object',
'properties': {
'categories': {
'type': 'array',
'items': {'type': 'string'},
},
'tags': {
'type': 'array',
'items': {'type': 'string'},
},
'screenshots': {
'type': 'array',
'items': {'type': 'string'},
},
'icon_url': {'type': 'string'},
},
'required': ['categories'],
}
APP_METADATA_JSON_SCHEMA = {
'type': 'object',
'properties': {
Expand Down Expand Up @@ -79,6 +98,14 @@
'required': ['description', 'host_path'],
},
},
'screenshots': {
'type': 'array',
'items': {'type': 'string'},
},
'categories': {
'type': 'array',
'items': {'type': 'string'},
},
},
'required': [
'name', 'train', 'version', 'app_version', 'title', 'description', 'home',
Expand Down
14 changes: 7 additions & 7 deletions apps_validation/validate_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import json
import yaml

from jsonschema import validate as json_schema_validate, ValidationError as JsonValidationError

from apps_ci.names import CACHED_VERSION_FILE_NAME
from apps_exceptions import ValidationErrors

from .json_schema_utils import APP_ITEM_JSON_SCHEMA
from .validate_app_version import validate_catalog_item_version_data, validate_catalog_item_version
from .utils import validate_key_value_types


def validate_catalog_item(catalog_item_path: str, schema: str, train_name: str, validate_versions: bool = True):
Expand Down Expand Up @@ -37,12 +39,10 @@ def validate_catalog_item(catalog_item_path: str, schema: str, train_name: str,
with open(os.path.join(catalog_item_path, 'item.yaml'), 'r') as f:
item_config = yaml.safe_load(f.read())

# TODO: Remove validate key value type function and have json schemas for all of this
validate_key_value_types(
item_config, (
('categories', list), ('tags', list, False), ('screenshots', list, False),
), verrors, f'{schema}.item_config'
)
try:
json_schema_validate(item_config, APP_ITEM_JSON_SCHEMA)
except JsonValidationError as e:
verrors.add(f'{schema}.item_config', f'Invalid format specified for item configuration: {e}')

cached_version_file_path = os.path.join(catalog_item_path, CACHED_VERSION_FILE_NAME)
if os.path.exists(cached_version_file_path):
Expand Down

0 comments on commit dd5309b

Please sign in to comment.