aboutsummaryrefslogtreecommitdiff
path: root/validators/bash/validate.sh
blob: a34c555f7d50e8585cabcfe7a5435a596d0ae260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash

# CLI validator for .gitinfo files
# Usage: ./validate.sh [file]
#        ./validate.sh              # validates .gitinfo in current directory
#        ./validate.sh path/to/.gitinfo

set -e

# Schema path (two levels up from validators/bash/)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCHEMA_PATH="$SCRIPT_DIR/../../gitinfo.schema.json"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Check for jq
if ! command -v jq &> /dev/null; then
    echo -e "${RED}Error: jq is required but not installed.${NC}" >&2
    echo "Install with: apt install jq / brew install jq / choco install jq" >&2
    exit 1
fi

# Strip JSONC comments using sed
strip_comments() {
    # Remove carriage returns (Windows line endings), single-line comments, and multi-line comments
    # Also remove trailing commas before } or ] (valid in JSONC, invalid in JSON)
    cat "$1" | tr -d '\r' | sed -e 's|//.*$||g' -e ':a;s|/\*.*\*/||g;ta' -e '/\/\*/,/\*\//d' | sed -e 's/,\s*}/}/g' -e 's/,\s*]/]/g'
}

# Validate URI format
validate_uri() {
    local uri="$1"
    if [[ "$uri" =~ ^https?:// ]]; then
        return 0
    fi
    return 1
}

# Validate email format
validate_email() {
    local email="$1"
    if [[ "$email" =~ ^[^[:space:]@]+@[^[:space:]@]+\.[^[:space:]@]+$ ]]; then
        return 0
    fi
    return 1
}

# Main validation function
validate() {
    local file="$1"
    local errors=()
    
    # Parse JSON
    local json
    if ! json=$(strip_comments "$file" | jq -c . 2>&1); then
        echo -e "${RED}Error parsing JSONC: $json${NC}" >&2
        exit 1
    fi
    
    # Load schema
    local schema
    if ! schema=$(jq -c . "$SCHEMA_PATH" 2>&1); then
        echo -e "${RED}Error parsing schema: $schema${NC}" >&2
        exit 1
    fi
    
    # Check if root is an object
    local type
    type=$(echo "$json" | jq -r 'type')
    if [[ "$type" != "object" ]]; then
        errors+=("root: expected object, got $type")
    fi
    
    # Get allowed properties from schema
    local allowed_props
    allowed_props=$(echo "$schema" | jq -r '.properties | keys[]')
    
    # Check for unknown properties
    local actual_props
    actual_props=$(echo "$json" | jq -r 'keys[]')
    for prop in $actual_props; do
        if ! echo "$allowed_props" | grep -qx "$prop"; then
            errors+=("root: unknown property \"$prop\"")
        fi
    done
    
    # Validate each property
    local schema_props
    schema_props=$(echo "$schema" | jq -r '.properties | to_entries[] | @base64')
    
    for entry in $schema_props; do
        local key format prop_type
        key=$(echo "$entry" | base64 -d | jq -r '.key')
        format=$(echo "$entry" | base64 -d | jq -r '.value.format // empty')
        prop_type=$(echo "$entry" | base64 -d | jq -r '.value.type')
        
        # Check if property exists
        local value
        value=$(echo "$json" | jq -r --arg k "$key" '.[$k] // empty')
        
        if [[ -n "$value" && "$value" != "null" ]]; then
            local actual_type
            actual_type=$(echo "$json" | jq -r --arg k "$key" '.[$k] | type')
            
            # Type check
            if [[ "$prop_type" == "string" && "$actual_type" != "string" ]]; then
                errors+=(".$key: expected string")
            elif [[ "$prop_type" == "array" && "$actual_type" != "array" ]]; then
                errors+=(".$key: expected array")
            fi
            
            # Format validation for strings
            if [[ "$actual_type" == "string" ]]; then
                if [[ "$format" == "uri" ]]; then
                    if ! validate_uri "$value"; then
                        errors+=(".$key: invalid URI \"$value\"")
                    fi
                elif [[ "$format" == "email" ]]; then
                    if ! validate_email "$value"; then
                        errors+=(".$key: invalid email \"$value\"")
                    fi
                fi
            fi
            
            # Validate array items
            if [[ "$actual_type" == "array" ]]; then
                local item_format
                item_format=$(echo "$entry" | base64 -d | jq -r '.value.items.format // empty')
                
                if [[ "$item_format" == "uri" ]]; then
                    local i=0
                    while IFS= read -r item; do
                        if ! validate_uri "$item"; then
                            errors+=(".${key}[$i]: invalid URI \"$item\"")
                        fi
                        ((i++))
                    done < <(echo "$json" | jq -r --arg k "$key" '.[$k][]?')
                fi
            fi
        fi
    done
    
    # Output results
    if [[ ${#errors[@]} -gt 0 ]]; then
        echo -e "${RED}Validation failed for $file:${NC}" >&2
        for error in "${errors[@]}"; do
            echo -e "  - $error" >&2
        done
        exit 1
    fi
    
    echo -e "${GREEN}✓ $file is valid${NC}"
    exit 0
}

# Main
FILE="${1:-.gitinfo}"

if [[ ! -f "$FILE" ]]; then
    echo -e "${RED}Error: File not found: $FILE${NC}" >&2
    exit 1
fi

if [[ ! -f "$SCHEMA_PATH" ]]; then
    echo -e "${RED}Error: Schema not found: $SCHEMA_PATH${NC}" >&2
    exit 1
fi

validate "$FILE"