From 4eeb92741c87785f050852f2aa46dae112057076 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Wed, 13 Mar 2024 14:07:52 +0800 Subject: [PATCH] Fix schema type checking Add type parameter for custom schema validation. --- src/core.ts | 10 +++++----- src/server.ts | 25 ++++++++++++------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/core.ts b/src/core.ts index 31e716e..852b39d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -37,20 +37,20 @@ interface ResourceFactory { route(n: string): this; } -export interface ResourceData { +export interface ResourceData { idAttr: string; itemName?: string; collectionName?: string; routeName?: string; newId(dataSource: DataSource): string | number | unknown; - schema: BaseSchema; + schema: T; throws404OnDeletingNotFound: boolean; checksSerializersOnDelete: boolean; } -export type Resource = ResourceData & ResourceFactory; +export type Resource = ResourceData & ResourceFactory; -export interface ResourceWithDataSource extends Resource { +export interface ResourceWithDataSource extends Resource { dataSource: DataSource; } @@ -62,7 +62,7 @@ interface IdParams { generationStrategy: GenerationStrategy; } -export const resource = (schema: T): Resource => { +export const resource = (schema: T): Resource => { let theIdAttr: string; let theItemName: string; let theCollectionName: string; diff --git a/src/server.ts b/src/server.ts index a7158dd..c9a0bf5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,9 +1,8 @@ import { application, DataSource, - Resource, resource, - valibot, + valibot as v, dataSources, serializers } from '.'; @@ -49,11 +48,11 @@ const TEXT_SERIALIZER_PAIR = { deserialize: (str: string) => str as T }; -const Piano = resource(valibot.object( +const Piano = resource(v.object( { - brand: valibot.string() + brand: v.string() }, - valibot.never() + v.never() )) .name('Piano') .id('id', { @@ -62,15 +61,15 @@ const Piano = resource(valibot.object( // TODO implement authentication and RBAC on each resource -const User = resource(valibot.object( +const User = resource(v.object( { - firstName: valibot.string(), - middleName: valibot.string(), - lastName: valibot.string(), - bio: valibot.string(), - createdAt: valibot.date() + firstName: v.string(), + middleName: v.string(), + lastName: v.string(), + bio: v.string(), + createdAt: v.date() }, - valibot.never() + v.never() )) .name('User') .fullText('bio') @@ -80,7 +79,7 @@ const User = resource(valibot.object( const app = application({ name: 'piano-service', - dataSource: (resource: Resource) => new dataSources.jsonlFile.DataSource(resource), + dataSource: (resource) => new dataSources.jsonlFile.DataSource(resource), }) .contentType('application/json', serializers.applicationJson) .contentType('text/json', serializers.textJson)