0.0.3 • Published 7 months ago

use-state-with-schema-validation v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

use-state-with-schema-validation

Motivation

With Schema you can force defined types, since typescript in runtime will be plain js and types will not be strictly throw errors when type missmatch happens.

In this case I created useState hook that will force the type using json schema specifications.

How to use

import { useStateWithSchema } from "use-state-with-schema-validation";

function Component() {
  const [user, setUser] = useStateWithSchema<Record<"id" | "name", string>>(
    {
      type: "object",
      required: ["id", "name"],
    },
    {
      id: "11-22",
      name: "John Doe",
    }
  );

  // Will work fine!
  setUser({
    id: "11-22",
    name: "John Doe",
  });

  // Will throw error since the value does not align with defined schema above
  setUser({
    id: "11-22",
  });
}

Made with 💜 by ibrahim

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago

0.0.0

7 months ago