-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-service/search): validate with valibot and drop yup
- Loading branch information
Showing
6 changed files
with
96 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
api-service/src/business-logic/standards/ladesa-spec/search/adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { inspect } from "util"; | ||
import * as LadesaTypings from "@ladesa-ro/especificacao"; | ||
import { PaginateQuery } from "nestjs-paginate"; | ||
import * as valibot from "valibot"; | ||
|
||
export type GenericListInputView = { | ||
queries: LadesaTypings.GenericSearchInputView; | ||
}; | ||
|
||
const undefinedOrType = (schema: valibot.BaseSchema<any, any, valibot.BaseIssue<any>>) => | ||
valibot.pipe( | ||
valibot.union([schema, valibot.null(), valibot.undefined()]), | ||
valibot.transform((value) => value ?? undefined), | ||
valibot.optional(schema), | ||
); | ||
|
||
const paginateQueryValidation = valibot.pipe( | ||
valibot.record(valibot.string(), valibot.unknown()), | ||
|
||
valibot.transform((input) => { | ||
if (input) { | ||
const allEntries = Object.entries(input); | ||
|
||
const otherEntries = allEntries.filter((entry) => !entry[0].startsWith("filter.")); | ||
|
||
const filterEntries = allEntries.filter((entry) => entry[0].startsWith("filter.")); | ||
|
||
return { | ||
...Object.fromEntries(otherEntries), | ||
|
||
filter: { | ||
...filterEntries.reduce((acc, [key, value]) => { | ||
const propertyPath = key.replace("filter.", ""); | ||
|
||
return { | ||
...acc, | ||
[propertyPath]: Array.isArray(value) ? value : [value], | ||
}; | ||
}, {}), | ||
}, | ||
}; | ||
} | ||
|
||
return input; | ||
}), | ||
|
||
valibot.object({ | ||
page: undefinedOrType(valibot.number()), | ||
limit: undefinedOrType(valibot.number()), | ||
|
||
sortBy: valibot.optional(valibot.array(valibot.tuple([valibot.string(), valibot.string()]))), | ||
|
||
searchBy: valibot.optional(valibot.array(valibot.string())), | ||
|
||
search: undefinedOrType(valibot.string()), | ||
|
||
filter: valibot.record(valibot.string(), valibot.array(valibot.string())), | ||
|
||
select: valibot.optional(valibot.array(valibot.string())), | ||
|
||
path: valibot.optional(valibot.string()), | ||
}), | ||
); | ||
|
||
export class PaginateQueryAdapter { | ||
static FromGenericListInputView(path: string, genericListInputView: GenericListInputView | null): PaginateQuery { | ||
const inputQueries = genericListInputView?.queries; | ||
|
||
const parseResult = valibot.parse(paginateQueryValidation, inputQueries); | ||
|
||
const parsedQueries = parseResult.output; | ||
|
||
const paginateQuery: PaginateQuery = { | ||
path, | ||
...parsedQueries, | ||
}; | ||
|
||
return paginateQuery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.