|
22 | 22 | echo "🔮 Summoning the JSON spirits to check your work..." |
23 | 23 | echo "📜 Using Schema: $SCHEMA_PATH" |
24 | 24 | echo "🎨 Output Format: $INPUT_ERROR_FORMAT" |
| 25 | + |
| 26 | +# Handle allowed extra properties |
| 27 | +FINAL_SCHEMA_PATH="$SCHEMA_PATH" |
| 28 | +if [ -n "$INPUT_ALLOWED_EXTRA_PROPERTIES" ]; then |
| 29 | + echo "✨ Allowing extra properties: $INPUT_ALLOWED_EXTRA_PROPERTIES" |
| 30 | + FINAL_SCHEMA_PATH="/tmp/modified_schema.json" |
| 31 | + |
| 32 | + # Use Python to modify the schema and add allowed extra properties |
| 33 | + if ! SCHEMA_PATH_ESC="$SCHEMA_PATH" \ |
| 34 | + FINAL_SCHEMA_PATH_ESC="$FINAL_SCHEMA_PATH" \ |
| 35 | + ALLOWED_PROPS_ESC="$INPUT_ALLOWED_EXTRA_PROPERTIES" \ |
| 36 | + python3 << 'PYTHON_EOF' |
| 37 | +import json |
| 38 | +import os |
| 39 | +import sys |
| 40 | +
|
| 41 | +try: |
| 42 | + schema_path = os.environ['SCHEMA_PATH_ESC'] |
| 43 | + final_schema_path = os.environ['FINAL_SCHEMA_PATH_ESC'] |
| 44 | + allowed_props_str = os.environ['ALLOWED_PROPS_ESC'] |
| 45 | +
|
| 46 | + # Read the original schema |
| 47 | + with open(schema_path, 'r') as f: |
| 48 | + schema = json.load(f) |
| 49 | +
|
| 50 | + # Ensure schema has the expected structure |
| 51 | + if not isinstance(schema, dict): |
| 52 | + print('❌ ERROR: Schema must be a JSON object', file=sys.stderr) |
| 53 | + sys.exit(1) |
| 54 | +
|
| 55 | + # Parse comma-separated list of allowed properties |
| 56 | + allowed_props = [prop.strip() for prop in allowed_props_str.split(',') if prop.strip()] |
| 57 | +
|
| 58 | + if not allowed_props: |
| 59 | + print('❌ ERROR: No valid property names found in allowed_extra_properties', file=sys.stderr) |
| 60 | + sys.exit(1) |
| 61 | +
|
| 62 | + # Ensure properties object exists |
| 63 | + if 'properties' not in schema: |
| 64 | + schema['properties'] = {} |
| 65 | +
|
| 66 | + # Add each allowed property to the schema's properties object |
| 67 | + # Use empty schema {} which allows any type |
| 68 | + for prop in allowed_props: |
| 69 | + if prop not in schema['properties']: |
| 70 | + schema['properties'][prop] = {} |
| 71 | +
|
| 72 | + # Write the modified schema |
| 73 | + with open(final_schema_path, 'w') as f: |
| 74 | + json.dump(schema, f, indent=2) |
| 75 | +
|
| 76 | + print(f'✅ Modified schema written to {final_schema_path}') |
| 77 | +except KeyError as e: |
| 78 | + print(f'❌ ERROR: Missing environment variable: {e}', file=sys.stderr) |
| 79 | + sys.exit(1) |
| 80 | +except FileNotFoundError: |
| 81 | + print(f'❌ ERROR: Schema file not found: {schema_path}', file=sys.stderr) |
| 82 | + sys.exit(1) |
| 83 | +except json.JSONDecodeError as e: |
| 84 | + print(f'❌ ERROR: Invalid JSON in schema file: {e}', file=sys.stderr) |
| 85 | + sys.exit(1) |
| 86 | +except Exception as e: |
| 87 | + print(f'❌ ERROR: Failed to modify schema: {e}', file=sys.stderr) |
| 88 | + sys.exit(1) |
| 89 | +PYTHON_EOF |
| 90 | + then |
| 91 | + echo "❌ Failed to modify schema for extra properties" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +fi |
| 95 | + |
25 | 96 | echo "" |
26 | 97 |
|
27 | 98 | # Run check-jsonschema with the corrected flag: --output-format |
28 | | -if check-jsonschema --schemafile "$SCHEMA_PATH" "$FILE_TO_VALIDATE" --output-format "$INPUT_ERROR_FORMAT"; then |
| 99 | +if check-jsonschema --schemafile "$FINAL_SCHEMA_PATH" "$FILE_TO_VALIDATE" --output-format "$INPUT_ERROR_FORMAT"; then |
29 | 100 | echo "" |
30 | 101 | echo "🥳 HOORAY! Your .zenodo.json is shiny and valid!" |
31 | 102 | echo "🏆 You're a hero of Open Science! 🚀✨" |
|
0 commit comments