Skip to content

Commit

Permalink
fix(deps): adapt new Joi types from nodejs-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jul 28, 2023
1 parent 75ea007 commit 669a2e6
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: default
name: ci

on: [push, workflow_dispatch]

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![npm](https://img.shields.io/npm/v/@naturalcycles/db-lib/latest.svg)](https://www.npmjs.com/package/@naturalcycles/db-lib)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![loc](https://badgen.net/codeclimate/loc/NaturalCycles/db-lib)](https://github.com/NaturalCycles/db-lib)
[![Actions](https://github.com/NaturalCycles/db-lib/workflows/default/badge.svg)](https://github.com/NaturalCycles/db-lib/actions)
[![Actions](https://github.com/NaturalCycles/db-lib/workflows/ci/badge.svg)](https://github.com/NaturalCycles/db-lib/actions)

Defines 3 things:

Expand Down
8 changes: 4 additions & 4 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AjvSchema,
AjvValidationError,
JoiValidationError,
ObjectSchemaTyped,
ObjectSchema,
TransformLogProgressOptions,
TransformMapOptions,
} from '@naturalcycles/nodejs-lib'
Expand Down Expand Up @@ -148,9 +148,9 @@ export interface CommonDaoCfg<
/**
* Joi, AjvSchema or ZodSchema is supported.
*/
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM> | ZodSchema<BM>
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM> | ZodSchema<TM>
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>

excludeFromIndexes?: (keyof DBM)[]

Expand Down
7 changes: 5 additions & 2 deletions src/commondao/common.dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import {
testItemTMSchema,
TEST_TABLE,
TestItemBM,
TestItemDBM,
TestItemTM,
testItemBMJsonSchema,
testItemDBMJsonSchema,
} from '../testing'
import { testItemBMJsonSchema, testItemDBMJsonSchema } from '../testing/test.model'
import { CommonDao } from './common.dao'
import { CommonDaoCfg, CommonDaoLogLevel, CommonDaoSaveOptions } from './common.dao.model'

let throwError = false

const db = new InMemoryDB()
const daoCfg: CommonDaoCfg<TestItemBM> = {
const daoCfg: CommonDaoCfg<TestItemBM, TestItemDBM, TestItemTM> = {
table: TEST_TABLE,
db,
dbmSchema: testItemDBMSchema,
Expand Down
6 changes: 3 additions & 3 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
AjvValidationError,
getValidationResult,
JoiValidationError,
ObjectSchemaTyped,
ObjectSchema,
ReadableTyped,
stringId,
transformBuffer,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ export class CommonDao<
*/
validateAndConvert<IN, OUT = IN>(
obj: Partial<IN>,
schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | ZodSchema<IN> | undefined,
schema: ObjectSchema<IN> | AjvSchema<IN> | ZodSchema<IN> | undefined,
modelType: DBModelType,
opt: CommonDaoOptions = {},
): OUT {
Expand Down Expand Up @@ -1231,7 +1231,7 @@ export class CommonDao<
})
} else {
// Joi
const vr = getValidationResult<IN, OUT>(obj as IN, schema, objectName)
const vr = getValidationResult(obj, schema, objectName)
error = vr.error
convertedValue = vr.value
}
Expand Down
4 changes: 2 additions & 2 deletions src/testing/test.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export const testItemBMSchema = objectSchema<TestItemBM>({
k3: numberSchema.optional(),
even: booleanSchema.optional(),
b1: binarySchema.optional(),
}).concat(baseDBEntitySchema)
}).concat(baseDBEntitySchema as any)

export const testItemDBMSchema = objectSchema<TestItemDBM>({
k1: stringSchema,
k2: stringSchema.allow(null).optional(),
k3: numberSchema.optional(),
even: booleanSchema.optional(),
b1: binarySchema.optional(),
}).concat(savedDBEntitySchema)
}).concat(savedDBEntitySchema as any)

export const testItemTMSchema = objectSchema<TestItemTM>({
k1: stringSchema,
Expand Down
13 changes: 11 additions & 2 deletions src/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import {
arraySchema,
booleanSchema,
integerSchema,
Joi,
objectSchema,
stringSchema,
} from '@naturalcycles/nodejs-lib'
import { CommonDBOptions, CommonDBSaveOptions } from '../db.model'
import { DBQuery, DBQueryFilter, dbQueryFilterOperatorValues, DBQueryOrder } from '../query/dbQuery'
import {
DBQuery,
DBQueryFilter,
DBQueryFilterOperator,
dbQueryFilterOperatorValues,
DBQueryOrder,
} from '../query/dbQuery'

export const commonDBOptionsSchema = objectSchema<CommonDBOptions>({
onlyCache: booleanSchema.optional(),
Expand All @@ -18,7 +25,9 @@ export const commonDBSaveOptionsSchema = objectSchema<CommonDBSaveOptions>({
excludeFromIndexes: arraySchema(stringSchema).optional(),
}).concat(commonDBOptionsSchema)

export const dbQueryFilterOperatorSchema = stringSchema.valid(...dbQueryFilterOperatorValues)
export const dbQueryFilterOperatorSchema = Joi.string<DBQueryFilterOperator>().valid(
...dbQueryFilterOperatorValues,
)

export const dbQueryFilterSchema = objectSchema<DBQueryFilter>({
name: stringSchema,
Expand Down
Loading

0 comments on commit 669a2e6

Please sign in to comment.