Skip to content

Commit

Permalink
fix(schema): Fix resolver data type and use new validation feature in…
Browse files Browse the repository at this point in the history
… test fixture (#2523)
  • Loading branch information
daffl authored Jan 8, 2022
1 parent 8c1b350 commit 1093f12
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
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

0 comments on commit 1093f12

Please sign in to comment.