mirror of
https://github.com/logto-io/logto.git
synced 2025-03-17 22:31:28 -05:00
feat: update field check rules (#854)
This commit is contained in:
parent
93b4966f59
commit
85a407c5f6
15 changed files with 42 additions and 56 deletions
|
@ -20,6 +20,7 @@
|
|||
"@fontsource/roboto-mono": "^4.5.7",
|
||||
"@logto/phrases": "^0.1.0",
|
||||
"@logto/react": "^0.1.7",
|
||||
"@logto/shared": "^0.1.0",
|
||||
"@logto/schemas": "^0.1.0",
|
||||
"@mdx-js/react": "^1.6.22",
|
||||
"@parcel/core": "^2.5.0",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { ConnectorType } from '@logto/schemas';
|
||||
import { phoneRegEx, emailRegEx } from '@logto/shared';
|
||||
import classNames from 'classnames';
|
||||
import ky from 'ky';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
|
@ -9,7 +10,6 @@ import Button from '@/components/Button';
|
|||
import FormField from '@/components/FormField';
|
||||
import TextInput from '@/components/TextInput';
|
||||
import Tooltip from '@/components/Tooltip';
|
||||
import { phoneRegEx, emailRegEx } from '@/utilities/regex';
|
||||
|
||||
import * as styles from './index.module.scss';
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
// TODO - LOG-1876: Share Regex Between Logto Core and Front-End
|
||||
export const emailRegEx = /^\S+@\S+\.\S+$/;
|
||||
export const phoneRegEx = /^\d+$/;
|
|
@ -31,6 +31,7 @@
|
|||
"@logto/connector-wechat": "^0.1.0",
|
||||
"@logto/connector-wechat-native": "^0.1.0",
|
||||
"@logto/phrases": "^0.1.0",
|
||||
"@logto/shared": "^0.1.0",
|
||||
"@logto/schemas": "^0.1.0",
|
||||
"@silverhand/essentials": "^1.1.6",
|
||||
"argon2": "^0.28.5",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { arbitraryObjectGuard, userInfoSelectFields } from '@logto/schemas';
|
||||
import { nameRegEx, passwordRegEx, usernameRegEx } from '@logto/shared';
|
||||
import { has } from '@silverhand/essentials';
|
||||
import pick from 'lodash.pick';
|
||||
import { InvalidInputError } from 'slonik';
|
||||
|
@ -21,7 +22,6 @@ import {
|
|||
updateUserById,
|
||||
} from '@/queries/user';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
import { nameRegEx, passwordRegEx, usernameRegEx } from '@/utils/regex';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { ConnectorDTO, Connectors, ConnectorType } from '@logto/schemas';
|
||||
import { emailRegEx, phoneRegEx } from '@logto/shared';
|
||||
import { object, string } from 'zod';
|
||||
|
||||
import {
|
||||
|
@ -14,7 +15,6 @@ import {
|
|||
import koaGuard from '@/middleware/koa-guard';
|
||||
import { updateConnector } from '@/queries/connector';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
import { emailRegEx, phoneRegEx } from '@/utils/regex';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
|
|
|
@ -526,9 +526,10 @@ describe('sessionRoutes', () => {
|
|||
});
|
||||
|
||||
it('throw error if username not valid', async () => {
|
||||
const usernameStartedWithNumber = '1username';
|
||||
const response = await sessionRequest
|
||||
.post('/session/register/username-password')
|
||||
.send({ username: '_', password: 'password' });
|
||||
.send({ username: usernameStartedWithNumber, password: 'password' });
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
|
||||
|
@ -547,7 +548,10 @@ describe('sessionRoutes', () => {
|
|||
});
|
||||
|
||||
it('throw error if username not valid', async () => {
|
||||
const response = await sessionRequest.get('/session/register/_u/existence');
|
||||
const usernameStartedWithNumber = '1username';
|
||||
const response = await sessionRequest.get(
|
||||
`/session/register/${usernameStartedWithNumber}/existence`
|
||||
);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
|
||||
|
|
|
@ -4,6 +4,13 @@ import path from 'path';
|
|||
import { ConnectorMetadata } from '@logto/connector-types';
|
||||
import { LogtoErrorCode } from '@logto/phrases';
|
||||
import { PasscodeType, userInfoSelectFields } from '@logto/schemas';
|
||||
import {
|
||||
redirectUriRegEx,
|
||||
emailRegEx,
|
||||
passwordRegEx,
|
||||
phoneRegEx,
|
||||
usernameRegEx,
|
||||
} from '@logto/shared';
|
||||
import { conditional } from '@silverhand/essentials';
|
||||
import pick from 'lodash.pick';
|
||||
import { Provider } from 'oidc-provider';
|
||||
|
@ -39,13 +46,6 @@ import {
|
|||
findUserByIdentity,
|
||||
} from '@/queries/user';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
import {
|
||||
redirectUriRegEx,
|
||||
emailRegEx,
|
||||
passwordRegEx,
|
||||
phoneRegEx,
|
||||
usernameRegEx,
|
||||
} from '@/utils/regex';
|
||||
|
||||
import { AnonymousRouter } from './types';
|
||||
|
||||
|
@ -68,8 +68,8 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
|
|||
'/session/sign-in/username-password',
|
||||
koaGuard({
|
||||
body: object({
|
||||
username: string().regex(usernameRegEx),
|
||||
password: string().regex(passwordRegEx),
|
||||
username: string(),
|
||||
password: string(),
|
||||
}),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
import { hexColorRegEx } from '@/utils/regex';
|
||||
|
||||
const invalidColors = [
|
||||
'',
|
||||
'#',
|
||||
'#1',
|
||||
'#2B',
|
||||
'#3cZ',
|
||||
'#4D9e',
|
||||
'#5f80E',
|
||||
'#6GHiXY',
|
||||
'#78Cb5dA',
|
||||
'rgb(0,13,255)',
|
||||
];
|
||||
|
||||
const validColors = ['#aB3', '#169deF'];
|
||||
|
||||
describe('hexColorRegEx', () => {
|
||||
test.each(validColors)('%p should succeed', async (validColor) => {
|
||||
expect(hexColorRegEx.test(validColor)).toBeTruthy();
|
||||
});
|
||||
test.each(invalidColors)('%p should fail', async (invalidColor) => {
|
||||
expect(hexColorRegEx.test(invalidColor)).toBeFalsy();
|
||||
});
|
||||
});
|
|
@ -47,6 +47,7 @@
|
|||
"dependencies": {
|
||||
"@logto/connector-types": "^0.1.0",
|
||||
"@logto/phrases": "^0.1.0",
|
||||
"@logto/shared": "^0.1.0",
|
||||
"zod": "^3.14.3"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Language } from '@logto/phrases';
|
||||
import { hexColorRegEx } from '@logto/shared';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
|
@ -76,8 +77,6 @@ export enum BrandingStyle {
|
|||
Logo_Slogan = 'Logo_Slogan',
|
||||
}
|
||||
|
||||
export const hexColorRegEx = /^#[\da-f]{3}([\da-f]{3})?$/i;
|
||||
|
||||
export const brandingGuard = z.object({
|
||||
primaryColor: z.string().regex(hexColorRegEx),
|
||||
isDarkModeEnabled: z.boolean(),
|
||||
|
|
9
packages/shared/src/file-utils.ts
Normal file
9
packages/shared/src/file-utils.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { existsSync, readFileSync } from 'fs';
|
||||
|
||||
export const getFileContents = (filePath: string, fallbackContent: string): string => {
|
||||
if (existsSync(filePath)) {
|
||||
return readFileSync(filePath, 'utf8');
|
||||
}
|
||||
|
||||
return fallbackContent;
|
||||
};
|
|
@ -1,9 +1,2 @@
|
|||
import { existsSync, readFileSync } from 'fs';
|
||||
|
||||
export const getFileContents = (filePath: string, fallbackContent: string): string => {
|
||||
if (existsSync(filePath)) {
|
||||
return readFileSync(filePath, 'utf8');
|
||||
}
|
||||
|
||||
return fallbackContent;
|
||||
};
|
||||
export * from './file-utils';
|
||||
export * from './regex';
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
export const emailRegEx = /^\S+@\S+\.\S+$/;
|
||||
export const phoneRegEx = /^\d+$/;
|
||||
export const usernameRegEx = /^.{3,}$/;
|
||||
export const nameRegEx = /^.{3,}$/;
|
||||
export const usernameRegEx = /^[A-Z_a-z-][\w-]*$/;
|
||||
export const nameRegEx = /^.+$/;
|
||||
export const passwordRegEx = /^.{6,}$/;
|
||||
export const redirectUriRegEx = /^https?:\/\//;
|
||||
export { hexColorRegEx } from '@logto/schemas';
|
||||
export const hexColorRegEx = /^#[\da-f]{3}([\da-f]{3})?$/i;
|
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
|
@ -482,6 +482,7 @@ importers:
|
|||
'@logto/phrases': ^0.1.0
|
||||
'@logto/react': ^0.1.7
|
||||
'@logto/schemas': ^0.1.0
|
||||
'@logto/shared': ^0.1.0
|
||||
'@mdx-js/react': ^1.6.22
|
||||
'@parcel/core': ^2.5.0
|
||||
'@parcel/transformer-mdx': ^2.5.0
|
||||
|
@ -537,6 +538,7 @@ importers:
|
|||
'@logto/phrases': link:../phrases
|
||||
'@logto/react': 0.1.7_react@17.0.2
|
||||
'@logto/schemas': link:../schemas
|
||||
'@logto/shared': link:../shared
|
||||
'@mdx-js/react': 1.6.22_react@17.0.2
|
||||
'@parcel/core': 2.5.0
|
||||
'@parcel/transformer-mdx': 2.5.0_qizmsa7ujgewn5fdmmvyi6cykq
|
||||
|
@ -602,6 +604,7 @@ importers:
|
|||
'@logto/connector-wechat-native': ^0.1.0
|
||||
'@logto/phrases': ^0.1.0
|
||||
'@logto/schemas': ^0.1.0
|
||||
'@logto/shared': ^0.1.0
|
||||
'@shopify/jest-koa-mocks': ^3.0.8
|
||||
'@silverhand/eslint-config': ^0.14.0
|
||||
'@silverhand/essentials': ^1.1.6
|
||||
|
@ -673,6 +676,7 @@ importers:
|
|||
'@logto/connector-wechat-native': link:../connector-wechat-native
|
||||
'@logto/phrases': link:../phrases
|
||||
'@logto/schemas': link:../schemas
|
||||
'@logto/shared': link:../shared
|
||||
'@silverhand/essentials': 1.1.7
|
||||
argon2: 0.28.5
|
||||
chalk: 4.1.2
|
||||
|
@ -818,6 +822,7 @@ importers:
|
|||
specifiers:
|
||||
'@logto/connector-types': ^0.1.0
|
||||
'@logto/phrases': ^0.1.0
|
||||
'@logto/shared': ^0.1.0
|
||||
'@silverhand/eslint-config': ^0.14.0
|
||||
'@silverhand/essentials': ^1.1.6
|
||||
'@silverhand/ts-config': ^0.14.0
|
||||
|
@ -836,6 +841,7 @@ importers:
|
|||
dependencies:
|
||||
'@logto/connector-types': link:../connector-types
|
||||
'@logto/phrases': link:../phrases
|
||||
'@logto/shared': link:../shared
|
||||
zod: 3.14.3
|
||||
devDependencies:
|
||||
'@silverhand/eslint-config': 0.14.0_hjjt7jwmhwqm7bjf55k5ihcdqq
|
||||
|
|
Loading…
Add table
Reference in a new issue