Skip to content

Commit

Permalink
Merge pull request #76 from codex-team/feat/schemas
Browse files Browse the repository at this point in the history
feat: add Note JSON schema for validate getNoteById and updateNoteContentByPublicId
  • Loading branch information
neSpecc authored Oct 21, 2023
2 parents 03f7980 + 848457c commit f9a4eac
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import UserRouter from '@presentation/http/router/user.js';
import AIRouter from '@presentation/http/router/ai.js';
import EditorToolsRouter from './router/editorTools.js';
import { UserSchema } from './schema/User.js';
import { NoteSchema } from './schema/Note.js';
import Policies from './policies/index.js';
import type { RequestParams, Response } from '@presentation/api.interface.js';
import NoteSettingsRouter from './router/noteSettings.js';
Expand Down Expand Up @@ -235,6 +236,7 @@ export default class HttpApi implements Api {
*/
private addSchema(): void {
this.server?.addSchema(UserSchema);
this.server?.addSchema(NoteSchema);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type NoteSettingsService from '@domain/service/noteSettings.js';
import { StatusCodes } from 'http-status-codes';
import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js';
import type { Note, NotePublicId } from '@domain/entities/note.js';
import { GetNoteSchema, NoteEditPayloadSchema } from '../schema/Note.js';

/**
* Get note by id options
Expand Down Expand Up @@ -78,16 +79,18 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
const noteSettingsService = opts.noteSettingsService;

/**
* Get note by id
* Get note by id with schema JSON (validate request params)
*/
fastify.get<{
Params: GetNoteByIdOptions,
Reply: Note | ErrorResponse
}>('/:id', async (request, reply) => {
}>('/:id', {
schema: {
params: GetNoteSchema,
},
}, async (request, reply) => {
const params = request.params;
/**
* TODO: Validate request params
*/

const { id } = params;

const note = await noteService.getNoteById(id);
Expand Down Expand Up @@ -153,14 +156,16 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
updatedAt: Note['updatedAt'],
}
}>('/', {
schema: {
body: NoteEditPayloadSchema,
},
config: {
policy: [
'authRequired',
],
},
}, async (request, reply) => {
/**
* @todo Validate request params
* @todo Check user access right
*/
const { id, content } = request.body;
Expand Down
52 changes: 52 additions & 0 deletions src/presentation/http/schema/Note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Note entity used for validation and serialization
*/
export const NoteSchema = {
$id: 'NoteSchema',
type: 'object',
properties: {
id: {
'type': 'string',
'pattern': '[a-zA-Z0-9-_]',
'maxLength': 10,
'minLength': 10,
},
'content': {
'type': 'object',
'properties': {
'time': {
'type':'number',
},
'blocks': {
'type':'array',
},
'version': {
'type':'string',
},
},
},
},
};

export const GetNoteSchema = {
$id: 'GetNoteSchema',
type: 'object',
properties: {
id: {
'$ref': 'NoteSchema#/properties/id',
},
},
};

export const NoteEditPayloadSchema = {
$id: 'NoteOptionSchema',
type: 'object',
properties: {
id: {
'$ref': 'NoteSchema#/properties/id',
},
'content': {
'$ref': 'NoteSchema#/properties/content',
},
},
};

0 comments on commit f9a4eac

Please sign in to comment.