Skip to content

Commit

Permalink
fix(RequiredAtom): make truly read-only atom instead of PrimitiveAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Mar 7, 2024
1 parent 09bd768 commit 9bb7517
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/atoms/schemaValidate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RESET } from "form-atoms";
import { zodValidate } from "form-atoms/zod";
import { Getter, WritableAtom, atom } from "jotai";
import type { Getter, WritableAtom } from "jotai";
import { atom } from "jotai";
import { atomWithDefault } from "jotai/utils";
import { ZodUndefined, z } from "zod";

Expand All @@ -12,16 +13,16 @@ export type ValidateConfig<
optionalSchema?: OptSchema | ((get: Getter) => OptSchema);
};

export type DefaultRequiredAtom = typeof defaultRequiredAtom;
export type WritableRequiredAtom = WritableAtom<
boolean,
[boolean | typeof RESET | ((prev: boolean) => boolean)],
void
>;

/**
* Read-only atom for default zodFields which all are required.
*/
const defaultRequiredAtom = atom(true as const);
const defaultRequiredAtom = atom(() => true);
defaultRequiredAtom.debugLabel = "zodField/defaultRequired";

export type ReadRequired = Parameters<typeof atomWithDefault<boolean>>[0];
Expand Down
5 changes: 3 additions & 2 deletions src/fields/list-field/listField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ZodAny, ZodArray, z } from "zod";

import { extendAtom } from "../../atoms/extendAtom";
import {
DefaultRequiredAtom,
ReadRequired,
ValidateConfig,
WritableRequiredAtom,
Expand All @@ -21,7 +22,7 @@ export type ExtendListAtom<Fields extends FormFields, Value, State> =
export type ListField<
Fields extends FormFields,
Value,
RequiredAtom = Atom<boolean>,
RequiredAtom = DefaultRequiredAtom,
> = ExtendListAtom<Fields, Value, { required: RequiredAtom }> & {
optional: (readRequired?: ReadRequired) => OptionalListField<Fields>;
};
Expand All @@ -31,7 +32,7 @@ export type ListFieldSubmitValue<
Required,
> = Required extends WritableRequiredAtom
? FormFieldSubmitValues<Fields>[]
: Required extends Atom<boolean>
: Required extends DefaultRequiredAtom
? [FormFieldSubmitValues<Fields>, ...FormFieldSubmitValues<Fields>[]]
: never;

Expand Down
11 changes: 6 additions & 5 deletions src/fields/zod-field/zodField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { ZodAny, ZodUndefined, z } from "zod";

import { extendAtom } from "../../atoms/extendAtom";
import {
ReadRequired,
ValidateConfig,
WritableRequiredAtom,
type DefaultRequiredAtom,
type ReadRequired,
type ValidateConfig,
type WritableRequiredAtom,
schemaValidate,
} from "../../atoms/schemaValidate";
import { ExtendFieldAtom, PrimitiveFieldAtom } from "../../atoms/types";
Expand All @@ -24,7 +25,7 @@ export type ZodFieldSubmitValue<Field> =
Field extends ZodField<infer Schema, infer OptSchema, infer Required>
? Required extends WritableRequiredAtom
? Schema["_output"] | OptSchema["_output"]
: Required extends Atom<boolean>
: Required extends DefaultRequiredAtom
? Schema["_output"]
: never
: never;
Expand All @@ -45,7 +46,7 @@ type RequiredZodField<
export type ZodField<
Schema extends z.Schema = ZodAny,
OptSchema extends z.Schema = ZodUndefined,
RequiredAtom = Atom<boolean>,
RequiredAtom = DefaultRequiredAtom,
> = ExtendFieldAtom<
Schema["_output"] | OptSchema["_output"],
{ required: RequiredAtom }
Expand Down

0 comments on commit 9bb7517

Please sign in to comment.