Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): Fix resolver data type and use new validation feature in test fixture #2523

Merged
merged 1 commit into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/schema/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Schema } from './schema';

export type PropertyResolver<T, V, C> = (
value: V|undefined,
obj: any,
obj: T,
context: C,
status: ResolverStatus<T, C>
) => Promise<V|undefined>;
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Resolver<T, C> {
stack: [...stack, resolver]
}

return resolver(value, data, context, resolverStatus);
return resolver(value, data as any, context, resolverStatus);
}

async resolve<D> (_data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T> {
Expand Down
13 changes: 8 additions & 5 deletions packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { memory, Service } from '@feathersjs/memory';

import {
schema, resolve, Infer, resolveResult,
queryProperty, resolveQuery,
validateQuery, validateData, resolveData
queryProperty, resolveQuery, resolveData
} from '../src';

export const userSchema = schema({
Expand All @@ -24,7 +23,7 @@ export const userResultSchema = schema({
$id: 'UserResult',
type: 'object',
additionalProperties: false,
required: ['id', ...userSchema.definition.required ],
required: ['id', ...userSchema.definition.required],
properties: {
...userSchema.definition.properties,
id: { type: 'number' }
Expand All @@ -35,6 +34,8 @@ export type User = Infer<typeof userSchema>;
export type UserResult = Infer<typeof userResultSchema>;

export const userDataResolver = resolve<User, HookContext<Application>>({
schema: userSchema,
validate: 'before',
properties: {
password: async () => {
return 'hashed';
Expand All @@ -43,6 +44,7 @@ export const userDataResolver = resolve<User, HookContext<Application>>({
});

export const userResultResolver = resolve<UserResult, HookContext<Application>>({
schema: userResultSchema,
properties: {
password: async (value, _user, context) => {
return context.params.provider ? undefined : value;
Expand Down Expand Up @@ -79,6 +81,7 @@ export type MessageResult = Infer<typeof messageResultSchema> & {
};

export const messageResultResolver = resolve<MessageResult, HookContext<Application>>({
schema: messageResultSchema,
properties: {
user: async (_value, message, context) => {
const { userId } = message;
Expand Down Expand Up @@ -114,6 +117,8 @@ export const messageQuerySchema = schema({
export type MessageQuery = Infer<typeof messageQuerySchema>;

export const messageQueryResolver = resolve<MessageQuery, HookContext<Application>>({
schema: messageQuerySchema,
validate: 'before',
properties: {
userId: async (value, _query, context) => {
if (context.params?.user) {
Expand All @@ -138,7 +143,6 @@ const app = feathers<ServiceTypes>()
.use('messages', memory());

app.service('messages').hooks([
validateQuery(messageQuerySchema),
resolveQuery(messageQueryResolver),
resolveResult(messageResultResolver)
]);
Expand All @@ -149,7 +153,6 @@ app.service('users').hooks([

app.service('users').hooks({
create: [
validateData(userSchema),
resolveData(userDataResolver)
]
});
Expand Down