Validation
Catch a malformed asset before it costs gas
DDO validation in v2 is entirely local. @oceanprotocol/ddo-js runs the document through
JSON-LD → RDF → SHACL against its bundled schemas, in process, with no network call. So it is
free, it is fast, and there is no reason not to run it.
v1 had a hand-rolled hasAllRequiredOceanDDOAttributes() that returned a boolean. This
replaces it and tells you which field failed.
Checking a document
const { , } = await ()
if (!) {
.()
{ providedBy: [ 'Less than 1 values' ] }
}Errors are keyed by field, with an array of messages each — the shape ddo-js reports.
Throwing instead
assertValid is the same check, but it throws a DdoValidationError carrying the field
errors rather than returning them.
try {
await ()
} catch () {
if ( instanceof ) {
.(.) // the same field map
}
}The error's message is already a readable one-liner, because it runs the errors through
formatValidationErrors.
You get this for free on publish
It runs twice, and the first pass is the one that saves you money. Before the NFT exists,
publish validates the document it would publish, with stand-ins for the addresses it does
not have yet — so a missing providedBy or a misshapen license stops the publish while
nothing has been minted. Once the tokens are real, the final document — the exact one that
gets signed and stored — is validated again.
Calling validate yourself is still worth it when you want to surface field errors in a form
before the user hits publish.
Reading the errors
const { } = await ()
// A one-line summary, suitable for a log or a toast.
()
'name: Less than 1 values; providedBy: Less than 1 values' // The raw SHACL report, when you need to know *why* a shape did not conform.
const = () The full N-Quads report is always attached under a fullReport key. It is far too noisy for
an error message, so formatValidationErrors filters it out and getValidationReport
retrieves it deliberately.
The DID check
Common failures
| Error | Cause |
|---|---|
providedBy: Less than 1 values | New in DDO v5, no v4 equivalent. Set it with setProvidedBy. |
name: Less than 1 values | Required. setName. |
credentials: Less than 1 values | Required on the asset and on every service since ddo-js 1.0.0. The builders always emit an object ({} when no rules are set), so this only appears for a hand-assembled or hand-patched DDO. |
id: did does not derive from… | The document's id does not match its nftAddress and chainId — see above. |