nsmcp::LogTool method validate (public)

 <instance of nsmcp::LogTool[i]> validate arguments

Defined in /usr/local/ns/tcl/nsmcp/log-tools.tcl

Validate the configured log operation's arguments. Decode the rooted JSON argument object and enforce this tool's option, range and combination constraints.

Parameters:
arguments (required)
Rooted ns_json typed triples for the tool's JSON argument object.
Returns:
An empty result on success; invalid input raises RPC error -32602.

Testcases:
No testcase defined.
Source code:
if {[lindex $arguments 1] ne "object"} {
    return -code error -errorcode {NSMCP RPC -32602} "Expected arguments object"
}
set allowed [dict get [dict create errors {severity literal limit} requestContext {identifier identifiers limit} results {jobHandle resultHandle cursor limit includePeerAddresses}] ${:operation}]
foreach {key type value} [lindex $arguments 2] {
    if {$key ni $allowed} {
        return -code error -errorcode {NSMCP RPC -32602} "Unknown log argument"
    }
    set expected string
    if {$key eq "limit"} {
        set expected number
    }
    if {$key eq "identifiers"} {
        set expected array
    }
    if {$key eq "includePeerAddresses"} {
        set expected boolean
    }
    if {$type ne $expected} {
        return -code error -errorcode {NSMCP RPC -32602} "Invalid log argument type"
    }
    if {$key eq "literal" && ($value eq "" || [string length [encoding convertto utf-8 $value]] > 1024
        || [regexp {[\x00\r\n]} $value])} {
        return -code error -errorcode {NSMCP RPC -32602} "Expected a nonempty single-line literal of at most 1024 UTF-8 bytes"
    }
    if {$key eq "identifiers"} {
        if {[llength $value] == 0 || [llength $value] > 60} {
            return -code error -errorcode {NSMCP RPC -32602} "Expected 1..20 identifiers"
        }
        foreach {i t id} $value {
            if {$t ne "string" || [string length [encoding convertto utf-8 $id]] > 512 || ![::nsmcp::logRecordParser validRequestIdentifier $id]} {
                return -code error -errorcode {NSMCP RPC -32602} "Expected dash-delimited log-thread identifiers"
            }
        }
    }
    if {$key eq "severity" && $value ni {Warning Error Fatal Bug}} {
        return -code error -errorcode {NSMCP RPC -32602} "Expected Warning, Error, Fatal or Bug"
    }
    if {$key in {jobHandle resultHandle} && ![regexp {^[0-9a-f]{64}$} $value]} {
        return -code error -errorcode {NSMCP RPC -32602} "Invalid log handle"
    }
    if {$key eq "limit" && (![string is integer -strict $value] || $value < 1 || $value > 100)} {
        return -code error -errorcode {NSMCP RPC -32602} "Limit must be between 1 and 100"
    }
    if {$key eq "cursor" && (![string is entier -strict $value] || $value < 0)} {
        return -code error -errorcode {NSMCP RPC -32602} "Invalid cursor"
    }
}
set values [:arguments $arguments]
if {${:operation} eq "requestContext"} {
    if {[dict exists $values identifier] == [dict exists $values identifiers]} {
        return -code error -errorcode {NSMCP RPC -32602} "Supply exactly one of identifier or identifiers"
    }
    if {[dict exists $values identifier] && (![::nsmcp::logRecordParser validRequestIdentifier [dict get $values identifier]] || [dict exists $values limit])} {
        return -code error -errorcode {NSMCP RPC -32602} "Expected a dash-delimited log-thread identifier; limit applies to batches"
    }
}
if {${:operation} eq "results"} {
    if {[dict exists $values jobHandle] == [dict exists $values resultHandle]} {
        return -code error -errorcode {NSMCP RPC -32602} "Supply exactly one of jobHandle (poll a job) or resultHandle (read a page)"
    }
    if {[dict exists $values jobHandle] && [dict size $values]!=1} {
        return -code error -errorcode {NSMCP RPC -32602} "Poll with jobHandle alone. Paging and includePeerAddresses apply to resultHandle pages; for access summaries set includePeerAddresses on access_summary"
    }
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: