Skip to content

Use problem detail to carry machine-readable details of errors in HTTP response content.

License

Notifications You must be signed in to change notification settings

nRFCloud/problem-detail

Repository files navigation

@nrfcloud/problem-detail

https://jsr.io/@nrfcloud/problem-detail

Helper function for implementing https://datatracker.ietf.org/doc/rfc9457/.

Use problem detail to carry machine-readable details of errors in HTTP response content.

Install with NPM

npx jsr add (--save-prod|--save-dev) @nrfcloud/problem-detail

Usage

import { formatTypeBoxErrors } from "@nrfcloud/validate-with-typebox";
import { HttpStatusCode, ProblemDetailError } from "@nrfcloud/problem-detail";
import type { ValueError } from "@sinclair/typebox/errors";
import type { MaybeResult } from "../domain/MaybeResult.ts";

export const validateJSON = <T>(
  json: Record<string, unknown>,
  validatejson: (json: Record<string, any>) =>
    | { value: T }
    | {
        errors: ValueError[];
      },
): MaybeResult<T> => {
  const maybeValid = validatejson(json);
  if ("errors" in maybeValid)
    return {
      error: new ProblemDetailError({
        title: "Invalid JSON response",
        status: HttpStatusCode.INTERNAL_SERVER_ERROR,
        detail: `Response validation failed: ${formatTypeBoxErrors(maybeValid.errors)}!`,
      }),
    };

  return { result: maybeValid.value };
};

About

Use problem detail to carry machine-readable details of errors in HTTP response content.

Resources

License

Stars

Watchers

Forks