@@ -8,40 +8,26 @@ export const template = ({
88 authStrategies,
99 type,
1010 relative
11- } : AuthenticationGeneratorContext ) => /* ts */ `import { jsonSchema, resolve } from '@feathersjs/schema'
12- import { Type, querySyntax } from '@feathersjs/typebox'
11+ } : AuthenticationGeneratorContext ) => /* ts */ `import { resolve } from '@feathersjs/schema'
12+ import { Type, getDataValidator, getValidator, querySyntax } from '@feathersjs/typebox'
1313import type { Static } from '@feathersjs/typebox'
1414${ authStrategies . includes ( 'local' ) ? `import { passwordHash } from '@feathersjs/authentication-local'` : '' }
1515
1616import type { HookContext } from '${ relative } /declarations'
1717import { dataValidator, queryValidator } from '${ relative } /schemas/validators'
1818
19- // Schema for the basic data model (e.g. creating new entries)
20- export const ${ camelName } DataSchema = Type.Object({
19+ // Main data model schema
20+ export const ${ camelName } Schema = Type.Object({
21+ ${ type === 'mongodb' ? '_id: Type.String()' : 'id: Type.Number()' } ,
2122 ${ authStrategies
2223 . map ( ( name ) =>
2324 name === 'local'
2425 ? ` email: Type.String(),
25- password: Type.String()`
26+ password: Type.Optional(Type. String() )`
2627 : ` ${ name } Id: Type.Optional(Type.String())`
2728 )
2829 . join ( ',\n' ) }
29- }, { $id: '${ upperName } Data', additionalProperties: false })
30- export type ${ upperName } Data = Static<typeof ${ camelName } DataSchema>
31- export const ${ camelName } DataValidator = jsonSchema.getDataValidator(${ camelName } DataSchema, dataValidator)
32- export const ${ camelName } DataResolver = resolve<${ upperName } Data, HookContext>({
33- properties: {
34- ${ authStrategies . includes ( 'local' ) ? `password: passwordHash({ strategy: 'local' })` : '' }
35- }
36- })
37-
38- // Schema for the data that is being returned
39- export const ${ camelName } Schema = Type.Intersect([
40- ${ camelName } DataSchema,
41- Type.Object({
42- ${ type === 'mongodb' ? '_id: Type.String()' : 'id: Type.Number()' }
43- })
44- ], { $id: '${ upperName } ' })
30+ },{ $id: '${ upperName } ', additionalProperties: false })
4531export type ${ upperName } = Static<typeof ${ camelName } Schema>
4632export const ${ camelName } Resolver = resolve<${ upperName } , HookContext>({
4733 properties: {}
@@ -54,14 +40,28 @@ export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext>({
5440 }
5541})
5642
43+ // Schema for the basic data model (e.g. creating new entries)
44+ export const ${ camelName } DataSchema = Type.Pick(${ camelName } Schema, [
45+ ${ authStrategies . map ( ( name ) => ( name === 'local' ? `'email', 'password'` : `'${ name } Id'` ) ) . join ( ', ' ) }
46+ ],
47+ { $id: '${ upperName } Data', additionalProperties: false }
48+ )
49+ export type ${ upperName } Data = Static<typeof ${ camelName } DataSchema>
50+ export const ${ camelName } DataValidator = getDataValidator(${ camelName } DataSchema, dataValidator)
51+ export const ${ camelName } DataResolver = resolve<${ upperName } , HookContext>({
52+ properties: {
53+ ${ authStrategies . includes ( 'local' ) ? `password: passwordHash({ strategy: 'local' })` : '' }
54+ }
55+ })
56+
5757// Schema for allowed query properties
58- export const ${ camelName } QuerySchema = Type.Intersect([
59- querySyntax(${ camelName } Schema),
60- // Add additional query properties here
61- Type.Object({})
58+ export const ${ camelName } QueryProperties = Type.Pick(${ camelName } Schema, ['${
59+ type === 'mongodb' ? '_id' : 'id'
60+ } ', ${ authStrategies . map ( ( name ) => ( name === 'local' ? `'email'` : `'${ name } Id'` ) ) . join ( ', ' ) }
6261])
62+ export const ${ camelName } QuerySchema = querySyntax(${ camelName } QueryProperties)
6363export type ${ upperName } Query = Static<typeof ${ camelName } QuerySchema>
64- export const ${ camelName } QueryValidator = jsonSchema. getValidator(${ camelName } QuerySchema, queryValidator)
64+ export const ${ camelName } QueryValidator = getValidator(${ camelName } QuerySchema, queryValidator)
6565export const ${ camelName } QueryResolver = resolve<${ upperName } Query, HookContext>({
6666 properties: {
6767 // If there is a user (e.g. with authentication), they are only allowed to see their own data
0 commit comments