Browse Source

Fix schema type checking

Add type parameter for custom schema validation.
master
TheoryOfNekomata 8 months ago
parent
commit
4eeb92741c
2 changed files with 17 additions and 18 deletions
  1. +5
    -5
      src/core.ts
  2. +12
    -13
      src/server.ts

+ 5
- 5
src/core.ts View File

@@ -37,20 +37,20 @@ interface ResourceFactory {
route(n: string): this; route(n: string): this;
} }


export interface ResourceData {
export interface ResourceData<T extends BaseSchema> {
idAttr: string; idAttr: string;
itemName?: string; itemName?: string;
collectionName?: string; collectionName?: string;
routeName?: string; routeName?: string;
newId(dataSource: DataSource): string | number | unknown; newId(dataSource: DataSource): string | number | unknown;
schema: BaseSchema;
schema: T;
throws404OnDeletingNotFound: boolean; throws404OnDeletingNotFound: boolean;
checksSerializersOnDelete: boolean; checksSerializersOnDelete: boolean;
} }


export type Resource = ResourceData & ResourceFactory;
export type Resource<T extends BaseSchema = any> = ResourceData<T> & ResourceFactory;


export interface ResourceWithDataSource extends Resource {
export interface ResourceWithDataSource<T extends BaseSchema = any> extends Resource<T> {
dataSource: DataSource; dataSource: DataSource;
} }


@@ -62,7 +62,7 @@ interface IdParams {
generationStrategy: GenerationStrategy; generationStrategy: GenerationStrategy;
} }


export const resource = <T extends BaseSchema>(schema: T): Resource => {
export const resource = <T extends BaseSchema>(schema: T): Resource<T> => {
let theIdAttr: string; let theIdAttr: string;
let theItemName: string; let theItemName: string;
let theCollectionName: string; let theCollectionName: string;


+ 12
- 13
src/server.ts View File

@@ -1,9 +1,8 @@
import { import {
application, application,
DataSource, DataSource,
Resource,
resource, resource,
valibot,
valibot as v,
dataSources, dataSources,
serializers serializers
} from '.'; } from '.';
@@ -49,11 +48,11 @@ const TEXT_SERIALIZER_PAIR = {
deserialize: <T>(str: string) => str as T deserialize: <T>(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') .name('Piano')
.id('id', { .id('id', {
@@ -62,15 +61,15 @@ const Piano = resource(valibot.object(


// TODO implement authentication and RBAC on each resource // 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') .name('User')
.fullText('bio') .fullText('bio')
@@ -80,7 +79,7 @@ const User = resource(valibot.object(


const app = application({ const app = application({
name: 'piano-service', name: 'piano-service',
dataSource: (resource: Resource) => new dataSources.jsonlFile.DataSource(resource),
dataSource: (resource) => new dataSources.jsonlFile.DataSource(resource),
}) })
.contentType('application/json', serializers.applicationJson) .contentType('application/json', serializers.applicationJson)
.contentType('text/json', serializers.textJson) .contentType('text/json', serializers.textJson)


Loading…
Cancel
Save