Expand description
Checking a payload against the parts of the OCPP specification the types themselves cannot carry.
Most spec limits are in the type: a field the schema bounds at
maxLength: 20 is a heapless::String<20>, so an over-long value is
rejected at construction and never reaches the wire. Two categories
escape that:
- Bounds too large to inline. A string bounded above 512 characters,
or an array above 16 elements, would make every message that contains
it enormous by value, so it is not stored at its spec ceiling. Under
the
allocfeature it becomes a plainalloc::string::String/alloc::vec::Vec, which has no bound at all; withoutallocit is aheaplesscollection at a caller-chosen capacity, which may sit either side of the spec’s. Either way the specification’s own limit is no longer enforced by the type.AuthorizeRequest::certificateis one:maxLength: 5500in the schema, a growableStringin theallocbuild. - Constraints no collection expresses.
minItems(usually1, i.e. “must not be empty”),minimum/maximumon numbers, and 1.6J’smultipleOfon charging limits. Aheapless::Vec<T, N>can say “at most N”; it cannot say “at least one”.
Validate covers exactly those. It is implemented for every generated
message and nested type, recurses through nested structs and arrays, and
reports the first violation with the JSON path that reaches it:
use ocpp_types::v201::AuthorizeRequest;
use ocpp_types::v201::common::{IdToken, IdTokenEnum};
use ocpp_types::validate::{Validate, ValidationErrorKind};
let request: AuthorizeRequest = AuthorizeRequest {
certificate: Some("-".repeat(6000)),
custom_data: None,
id_token: IdToken {
additional_info: None,
custom_data: None,
id_token: heapless::String::try_from("ABC123").unwrap(),
r#type: IdTokenEnum::ISO14443,
},
iso15118_certificate_hash_data: None,
};
let error = request.validate().unwrap_err();
assert_eq!(
error.kind(),
ValidationErrorKind::TooLong { len: 6000, max: 5500 },
);§What it does not check
Only what the JSON schemas state. Cross-field rules from the
specification’s prose (a TransactionEventRequest with
eventType: Started must carry a triggerReason, and so on) are out of
scope, as is anything about the caller’s own CustomDataType payload or
2.x’s deliberately untyped DataTransfer.data – the spec constrains
neither, so neither is visited. Validating those is the caller’s, and
nothing stops a caller from implementing Validate for their own
payload type.
Validation is never automatic: nothing on the serialize path calls it,
so a payload is checked exactly when you ask for it. Sending is the
natural place — an over-long field comes back from the peer as a
CALLERROR you cannot correlate to a field, whereas
ValidationError names it.
Structs§
- Validation
Error - A schema constraint that a payload broke, and the path to the value that broke it.
Enums§
- Constraint
Class - Which of OCPP’s two constraint categories a violation falls into, and so
which
CALLERRORcode answers it. - Path
Segment - One step of the JSON path to a failing value: an object key, or a position within an array.
- Validation
Error Kind - What was wrong with the value.
Constants§
- MAX_
PATH_ DEPTH - How many path segments a
ValidationErrorcarries before it starts truncating (seeValidationError::path_truncated).
Traits§
- Validate
- A type that can be checked against the constraints its schema states.
Functions§
- check_
max_ f64 - Rejects a number above its schema’s
maximum.NaNpasses, as incheck_min_f64. - check_
max_ i64 - Rejects an integer above its schema’s
maximum. - check_
max_ items - Rejects an array longer than its schema’s
maxItems. - check_
max_ length - Rejects a string longer than
maxcharacters. - check_
min_ f64 - Rejects a number below its schema’s
minimum. - check_
min_ i64 - Rejects an integer below its schema’s
minimum. - check_
min_ items - Rejects an array shorter than its schema’s
minItems. - check_
multiple_ of - Rejects a number that is not a whole multiple of
multiple, within [MULTIPLE_OF_TOLERANCE].