Function fromObject

  • Asynchronously validates a data object against a provided schema.

    This function serves as a high-level utility for ensuring that data conforms to a specified schema using the validator defined in the schema's structure. It is useful for runtime validation where the schema and data are dynamically provided.

    Example


    import { fromObject } from "@mrspartak/config";
    import * as z from "zod";

    const schema = z.object({
    name: z.string(),
    age: z.number(),
    isStudent: z.boolean(),
    });

    const data = await fromObject({
    data: { name: "Alice", age: 25, isStudent: true },
    schema,
    });

    console.log(data); // { name: 'Alice', age: 25, isStudent: true }
    const name: string = data.name; // OK

    Type Parameters

    Parameters

    • params: {
          data: unknown;
          schema: $Validator;
      }

      The parameters object.

      • data: unknown

        The data object to validate.

      • schema: $Validator

        The validator schema against which to validate the data.

    Returns Promise<inferValidator<$Validator>["out"]>

    A promise that resolves with the validated data.

    Throws

    Throws an error if validation fails.