This commit is contained in:
2023-10-05 17:01:45 +03:00
parent 5cc8f45f90
commit a68f3964e1
9 changed files with 72 additions and 45 deletions

View File

@@ -22,7 +22,7 @@ export type TUiFieldSelectOption = {
*/
export const fieldSchema = <T extends ZodTypeAny>(
base: T,
args?: TFormSchemaCtrlArgs
args: TFormSchemaCtrlArgs = {}
) => base.describe(FormSchemaCtrl.toString(args, base.description))
/**

View File

@@ -73,7 +73,7 @@ export class BaseFormModel<T extends object = {}> implements IFormModel<T> {
let items: TNotificationItem[] = []
for (const code in this._errors) {
const text = this._errors[code]
items.push({ code, text })
if (text) items.push({ code, text })
}
return items
}
@@ -94,7 +94,9 @@ export class BaseFormModel<T extends object = {}> implements IFormModel<T> {
}
setValidError(code: string, text: string) {
this._errors[code] = code + ' - ' + text
let obj:any = {}
obj[code] = code + ' - ' + text
this._errors = Object.assign(this._errors, obj)
}
mergeObj(obj: any) {

View File

@@ -30,7 +30,7 @@ export class DataResultEntity<T> implements IDataResult<T> {
this.setData()
}
setData(data: T = null, pagination?: TPagination): void {
setData(data: T | null = null, pagination?: TPagination): void {
if (data !== null) {
this.data = data
}

View File

@@ -5,7 +5,6 @@ import { isEqual } from '../utils'
import { Pagination, paginationQuerySchema } from './pagination'
import { bFieldsSchema, cFieldsSchema, fieldSchema } from '../forms'
export const querySchema = cFieldsSchema
.pick({ q: true })
.extend(paginationQuerySchema.shape)
@@ -14,6 +13,8 @@ export const querySchema = cFieldsSchema
label: 'Сортировка'
})
})
.partial()
.describe('Параметры базового запроса')
export type TQuery = z.infer<typeof querySchema>
@@ -32,39 +33,32 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
sort?: string
constructor(query?: T) {
let queryCopy = Object.assign({}, query)
let queryCopy: T = Object.assign({}, query)
// Pagination.
this.setPagination(queryCopy)
for (const key of Object.keys(this.pagination)) {
if (queryCopy[key]) delete queryCopy[key]
if (this.pagination) {
// Delete pagination props.
const paginationKeys = Object.keys(this.pagination) as [keyof T]
for (const key of paginationKeys) {
if (queryCopy[key]) delete queryCopy[key]
}
}
// Sort.
if (queryCopy.sort) {
this.sort = queryCopy.sort
delete queryCopy.sort
queryCopy.sort = undefined
}
// Obj.
this.obj = queryCopy
}
setPagination(pagination?: TPagination) {
setPagination(pagination?: Partial<TPagination>) {
this.pagination = new Pagination(pagination).toObject()
}
static getQuery <T extends TQuery = {}>(filter: TFindFilter<T>): T {
let query: any = {}
for(const key of Object.keys(filter.obj)) {
query[key] = filter.obj[key]
}
if (filter.pagination?.page) query.page = filter.pagination.page
if (filter.pagination?.limit) query.limit = filter.pagination.limit
if (filter.sort) query.sort = filter.sort
return query
}
toObject(): TFindFilter<T> {
return {
obj: this.obj,

View File

@@ -21,9 +21,13 @@ export const paginationSchema = cFieldsSchema
export type TPagination = z.infer<typeof paginationSchema>
export const paginationQuerySchema = paginationSchema.pick({
page: true, limit: true
})
export const paginationQuerySchema = paginationSchema
.pick({
page: true,
limit: true
})
.partial()
.describe('Параметры разбиения на страницы')
export type TPaginationQuery = z.infer<typeof paginationQuerySchema>
@@ -113,7 +117,6 @@ export class Pagination implements TPagination {
page: this.page,
limit: this.limit,
total: this.total,
skip: this.skip,
pages: this.pages
}

View File

@@ -1,10 +1,12 @@
export const isEqual = <T extends object>(objects: T[]) => {
for (let i = 0; i < objects.length; i++) {
const obj1 = objects[i]
const obj2 = objects[i + 1]
const obj1: T = objects[i]
const obj2: T = objects[i + 1]
if (obj2) {
const keys1 = Object.keys(obj1)
const keys2 = Object.keys(obj2)
const keys1 = Object.keys(obj1) as [keyof T]
const keys2 = Object.keys(obj2) as [keyof T]
if (keys1.length !== keys2.length) {
return false
@@ -16,7 +18,7 @@ export const isEqual = <T extends object>(objects: T[]) => {
return false
}
} else {
if (!isEqual([obj1[key], obj2[key]])) {
if (!isEqual([obj1[key], obj2[key]] as T[])) {
return false
}
}