130 lines
3.5 KiB
Text
130 lines
3.5 KiB
Text
|
|
# Validation helpers — parameter, path, IP, port, settings checks (no IO, no config)
|
||
|
|
|
||
|
|
export def validate-required [
|
||
|
|
value: any
|
||
|
|
name: string
|
||
|
|
context?: string
|
||
|
|
]: nothing -> bool {
|
||
|
|
if ($value | is-empty) {
|
||
|
|
print $"🛑 Required parameter '($name)' is missing or empty"
|
||
|
|
if ($context | is-not-empty) {
|
||
|
|
print $"Context: ($context)"
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def validate-path [
|
||
|
|
path: string
|
||
|
|
context?: string
|
||
|
|
--must-exist
|
||
|
|
]: nothing -> bool {
|
||
|
|
if ($path | is-empty) {
|
||
|
|
print "🛑 Path parameter is empty"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
if $must_exist and not ($path | path exists) {
|
||
|
|
print $"🛑 Path '($path)' does not exist"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def validate-command [
|
||
|
|
command: string
|
||
|
|
context?: string
|
||
|
|
]: nothing -> bool {
|
||
|
|
let cmd_exists = (^bash -c $"type -P ($command)" | complete)
|
||
|
|
if $cmd_exists.exit_code != 0 {
|
||
|
|
print $"🛑 Command '($command)' not found in PATH"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def validate-ip [
|
||
|
|
ip: string
|
||
|
|
context?: string
|
||
|
|
]: nothing -> bool {
|
||
|
|
let ip_parts = ($ip | split row ".")
|
||
|
|
if ($ip_parts | length) != 4 {
|
||
|
|
print $"🛑 Invalid IP address format: ($ip)"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
let valid_parts = ($ip_parts | each {|part| let num = ($part | into int); $num >= 0 and $num <= 255 })
|
||
|
|
if not ($valid_parts | all {|v| $v}) {
|
||
|
|
print $"🛑 Invalid IP address values: ($ip)"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def validate-port [
|
||
|
|
port: int
|
||
|
|
context?: string
|
||
|
|
]: nothing -> bool {
|
||
|
|
if $port < 1 or $port > 65535 {
|
||
|
|
print $"🛑 Invalid port number: ($port). Must be between 1 and 65535"
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def validate-settings [
|
||
|
|
settings: record
|
||
|
|
required_fields: list
|
||
|
|
context?: string
|
||
|
|
]: nothing -> bool {
|
||
|
|
let missing_fields = ($required_fields | where {|field|
|
||
|
|
not ($field in $settings) or (($settings | get $field) | is-empty)
|
||
|
|
})
|
||
|
|
if ($missing_fields | length) > 0 {
|
||
|
|
print "🛑 Missing required settings fields:"
|
||
|
|
$missing_fields | each {|field| print $" - ($field)"}
|
||
|
|
if ($context | is-not-empty) { print $"Context: ($context)" }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def check-required [value: any, name: string]: nothing -> bool {
|
||
|
|
if ($value | is-empty) {
|
||
|
|
print $"🛑 Required parameter '($name)' is missing or empty"
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def check-path [path: string]: nothing -> bool {
|
||
|
|
if ($path | is-empty) {
|
||
|
|
print "🛑 Path parameter is empty"
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def check-path-exists [path: string]: nothing -> bool {
|
||
|
|
if not ($path | path exists) {
|
||
|
|
print $"🛑 Path '($path)' does not exist"
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
true
|
||
|
|
}
|
||
|
|
|
||
|
|
export def safe-run [command: closure, context: string]: nothing -> any {
|
||
|
|
let result = (do { do $command } | complete)
|
||
|
|
if $result.exit_code != 0 {
|
||
|
|
print $"⚠️ Warning: Error in ($context): ($result.stderr)"
|
||
|
|
null
|
||
|
|
} else {
|
||
|
|
$result.stdout
|
||
|
|
}
|
||
|
|
}
|