Skip to content

Commit

Permalink
feat: rename cast to defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 9, 2024
1 parent 6225fca commit 5731d3d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/form/formSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export function useFormSnapshots<TForm extends FormObject, TOutput extends FormO
const provided = toValue(provider);
if (isPromise(provided)) {
provided.then(resolved => {
const inits = opts?.schema?.cast?.(resolved) ?? resolved;
const inits = opts?.schema?.defaults?.(resolved) ?? resolved;
initials.value = cloneDeep(inits || {}) as TForm;
originals.value = cloneDeep(inits || {}) as TForm;
opts?.onAsyncInit?.(cloneDeep(inits));
});
} else {
const inits = opts?.schema?.cast?.(provided || {}) ?? provided;
const inits = opts?.schema?.defaults?.(provided || ({} as TForm)) ?? provided;
initials.value = cloneDeep(inits || {}) as TForm;
originals.value = cloneDeep(inits || {}) as TForm;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/form/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,10 @@ describe('validation', () => {
const { values } = await renderSetup(() => {
return useForm({
schema: {
cast: () => ({ test: 'foo' }),
defaults: () => ({ test: 'foo' }),
async parse() {
return {
errors: [],
output: {
test: 'foo',
},
};
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/typedSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface TypedSchemaContext {

export interface TypedSchema<TInput = any, TOutput = TInput> {
parse(values: TInput, context?: TypedSchemaContext): Promise<{ output?: TOutput; errors: TypedSchemaError[] }>;
cast?(values: Partial<TInput>): TInput;
defaults?(values: TInput): TInput;
}

export type InferOutput<TSchema extends TypedSchema> =
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-yup/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function defineSchema<TSchema extends Schema, TOutput = InferType<TSchema
return { errors: Object.values(errors) };
}
},
cast(values) {
defaults(values) {

Check failure on line 50 in packages/schema-yup/src/index.ts

View workflow job for this annotation

GitHub Actions / ts-lint-test

Parameter 'values' implicitly has an 'any' type.
try {
return yupSchema.cast(values);
} catch {
Expand Down

0 comments on commit 5731d3d

Please sign in to comment.