A tool (also website) validating a Hydra API against possible mistakes
This is a monorepo. Check the packages for more information:
Visit https://analyse.hypermedia.app
More info in validator-ui package.
It is also possible to run verification of a Hydra API from the command line. This may be useful to run sanity checks locally during development or as part of a CI pipeline.
More info in validator-cli package.
Code shared between other packages.
More info in validator-core package.
Static analyser of a Hydra API. Checks the triples and hypermedia controls for potential errors.
More info in validator-analyse package.
End-to-end rules executed against a Hydra API.
More info in validator-e2e package.
Each verification check is a parameterless function, which returns the result and optionally an array of child checks. It can also be async.
export interface CheckResult {
result?: IResult;
results?: IResult[];
nextChecks?: checkChain[];
sameLevel?: boolean;
}
export type checkChain = (this: Context) => Promise<CheckResult> | CheckResultTo implement a check, you'd usually wrap the check function in a closure to pass in dependencies. This way checks are chained to run when the previous check succeeds.
Here's an example which could verify that the input is a number and then pass on to a check for parity.
// isnum.ts
import {checkChain, Result} from '../check';
import iseven from './iseven';
export default function (maybeNum: any): checkChain {
return () => {
if (Number.isInteger(maybeNum)) {
return {
result: Result.Success('Value is number'),
nextChecks: [ iseven(maybeNum) ]
}
}
return {
result: Result.Failure('Value is not a number')
}
}
}
// iseven.ts
import {checkChain, Result} from '../check';
export default function (num: number): checkChain {
return () => {
const result = num % 2 === 0
? Result.Success(`Number ${num} is even`)
: Result.Failure(`Number ${num} is odd`)
return { result }
}
}Any new check must be added to existing chains in a similar matter to how iseven follows isnum.
Results can be reported with four factory methods: Result.Succes, Result.Failure, Result.Warning
and Result.Informational.
Note that there is no restriction for chaining. Additional checks can follow a successful check as well as failed ones.
To create a plugin, create a project called hydra-validator-uber-check, where uber-check will become
the CLI command.
In the package main module, export a default checkChain function which will be called first from
the CLI.
Optionally, add export const options, which exports an array of command line parameters. Here's an example
of one such option:
{
flags: '-l, --log-level <logLevel>',
description: 'Minimum log level',
defaultValue: 'INFO'
}
An object with the values passed from the command line will be provided as the second argument to the
main checkChain function.