0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(schemas,core): enable min string length guard (#3850)

enable min string length guard to all non-null string typed field in Guards
This commit is contained in:
simeng-li 2023-05-17 11:28:22 +08:00 committed by GitHub
parent 7144163530
commit 21d1fa42c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -65,6 +65,7 @@ export const generateSchema = ({ name, fields }: TableWithType) => {
`const guard: Guard<${modelName}> = z.object({`,
...fields.map(
// eslint-disable-next-line complexity
({ name, type, isArray, isEnum, nullable, tsType, isString, maxLength, hasDefaultValue }) => {
if (tsType) {
return ` ${camelcase(name)}: ${camelcase(tsType)}Guard${conditionalString(
@ -73,6 +74,11 @@ export const generateSchema = ({ name, fields }: TableWithType) => {
}
return ` ${camelcase(name)}: z.${isEnum ? `nativeEnum(${type})` : `${type}()`}${
// Non-nullable strings should have a min length of 1
conditionalString(
isString && !(nullable || hasDefaultValue || name === tenantId) && `.min(1)`
)
}${
// String types value in DB should have a max length
conditionalString(isString && maxLength && `.max(${maxLength})`)
}${conditionalString(isArray && '.array()')}${conditionalString(

View file

@ -22,7 +22,7 @@ export const buildDemoAppDataForTenant = (tenantId: string): Application => ({
tenantId,
id: demoAppApplicationId,
name: 'Live Preview',
secret: '',
secret: 'N/A',
description: 'Preview for Sign-in Experience.',
type: ApplicationType.SPA,
oidcClientMetadata: { redirectUris: [], postLogoutRedirectUris: [] },