Skip to content

Conversation

@jorisre
Copy link
Member

@jorisre jorisre commented Jan 5, 2021

Last step before starting new features :)

Before
Screen Shot 2021-01-02 at 19 20 53

After
Screen Shot 2021-01-02 at 19 20 32

@jorisre jorisre changed the base branch from master to beta January 5, 2021 22:24
@jorisre jorisre force-pushed the improve-resolvers-typescript branch from 98633da to 2721382 Compare January 6, 2021 10:42
@jorisre jorisre force-pushed the improve-resolvers-typescript branch from 3d3d46d to e4c750a Compare January 6, 2021 22:17
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2021

Size Change: +88 B (+1%)

Total Size: 14.3 kB

Filename Size Change
superstruct/dist/superstruct.js 991 B +24 B (+2%)
superstruct/dist/superstruct.modern.js 928 B +3 B (0%)
superstruct/dist/superstruct.module.js 1.01 kB +27 B (+3%)
superstruct/dist/superstruct.umd.js 1.09 kB +25 B (+2%)
vest/dist/vest.js 494 B +2 B (0%)
vest/dist/vest.modern.js 442 B +2 B (0%)
vest/dist/vest.module.js 506 B +3 B (+1%)
vest/dist/vest.umd.js 592 B +2 B (0%)
ℹ️ View Unchanged
Filename Size Change
dist/resolvers.js 169 B 0 B
dist/resolvers.modern.js 177 B 0 B
dist/resolvers.module.js 182 B 0 B
dist/resolvers.umd.js 260 B 0 B
joi/dist/joi.js 604 B 0 B
joi/dist/joi.modern.js 503 B 0 B
joi/dist/joi.module.js 616 B 0 B
joi/dist/joi.umd.js 704 B 0 B
yup/dist/yup.js 695 B 0 B
yup/dist/yup.modern.js 602 B 0 B
yup/dist/yup.module.js 704 B 0 B
yup/dist/yup.umd.js 791 B 0 B
zod/dist/zod.js 534 B 0 B
zod/dist/zod.modern.js 485 B 0 B
zod/dist/zod.module.js 547 B 0 B
zod/dist/zod.umd.js 637 B 0 B

compressed-size-action

@jorisre jorisre requested a review from bluebill1049 January 7, 2021 11:59
@jorisre jorisre marked this pull request as ready for review January 7, 2021 12:00
@jorisre jorisre changed the title V2 - TypeScript improvements V2 - TypeScript/Tests improvements + Vest fix Jan 8, 2021
@jorisre
Copy link
Member Author

jorisre commented Jan 8, 2021

@bluebill1049 PR ready for review ;)

birthYear: 'birthYear',
};

const result = await joiResolver(schema)(data, undefined, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a quick note, going to change this true to an object.

{
  isValidateAllFieldCriteria: boolean,
  field: {
    name: string,
  },
  // potential more
}

maybe call it formContext, not sure if that's going to be too confusing, but we figure out a good name later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sound good to me. I'll change it in another PR.

Does field contains ref too ?
Do you think we have to add an option to allow users to validate only the current instead all field ? (except on submit event)
Or users have to create a custom resolver ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does field contains ref too ?

Yes.

Do you think we have to add an option to allow users to validate only the current instead all field ? (except on submit event)

Yes, That would be awesome. I am not sure how easy it is going to be for all resolvers.


const result = await joiResolver(schema)(data);

expect(result).toMatchSnapshot();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: (don't have to change as well) maybe be specific with the error? I normally use snapshots for a successful responses.

Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love those:

  • Tests
  • Type improvement
  • Structure improvement

Awesome stuff!! 🎖

} from 'react-hook-form';
import type { AsyncValidationOptions, Schema } from 'joi';

export type Resolver = <T extends Schema>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the type in the react-hook-form repo:

I think maybe it's better to leave that at react-hook-form and centralize there?

export type Resolver<
  TFieldValues extends FieldValues = FieldValues,
  TContext extends object = object
> = (
  values: UnpackNestedValue<TFieldValues>,
  context?: TContext,
  validateAllFieldCriteria?: boolean,
) => Promise<ResolverResult<TFieldValues>> | ResolverResult<TFieldValues>;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the same type

From react-hook-form:

export type Resolver<
  TFieldValues extends FieldValues = FieldValues,
  TContext extends object = object
> = (
  values: UnpackNestedValue<TFieldValues>,
  context?: TContext,
  validateAllFieldCriteria?: boolean,
) => Promise<ResolverResult<TFieldValues>> | ResolverResult<TFieldValues>;

And from resolvers:

export type Resolver = <T extends Schema>(
  schema: T,
  options?: AsyncValidationOptions,
) => <TFieldValues extends FieldValues, TContext>(
  values: UnpackNestedValue<TFieldValues>,
  context?: TContext,
  validateAllFieldCriteria?: boolean,
) => Promise<ResolverResult<TFieldValues>>;

The generic type isn't placed at the same point. This update improve resolver's return type. But I'm open for a better idea 😉

I tried to centralized but it breaks severals things: react-hook-form/react-hook-form#3821

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good, I realize that after reviewing the rest of the resolvers.

} from 'react-hook-form';
import { validate, Struct } from 'superstruct';

type Options = Parameters<typeof validate>[2];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see why you need to leave those type here, that's fine as well. probably need to include that in the doc, so user don't get confused where the type is, perhaps we should remove that type from the core repo.

};

export const vestResolver = <TFieldValues extends FieldValues>(
schema: ICreateResult,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

_: any = {},
export const vestResolver: Resolver = (schema, _ = {}) => async (
values,
_context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@jorisre jorisre merged commit b7dd0e6 into beta Jan 9, 2021
@jorisre jorisre mentioned this pull request Jan 9, 2021
5 tasks
@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.0.0-beta.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jorisre jorisre deleted the improve-resolvers-typescript branch February 6, 2021 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants