From 85fa9976488d16fa8da8c62af47b995662e5b25f Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Wed, 25 Jun 2025 13:01:46 +0200 Subject: [PATCH] more schemas --- shared/src/schema/user.schema.test.ts | 45 ++++++++++++++++++++------- shared/src/schema/user.schema.ts | 17 ++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/shared/src/schema/user.schema.test.ts b/shared/src/schema/user.schema.test.ts index d8f91cb1e..015309dfd 100644 --- a/shared/src/schema/user.schema.test.ts +++ b/shared/src/schema/user.schema.test.ts @@ -1,16 +1,7 @@ -import { aliasSchema } from './user.schema' -import { describe, it, expect, beforeEach, mock, jest } from 'bun:test' - - -mock.module('database', () => ({ - aliasExists: jest.fn(), -})) +import { aliasSchema, firstNameSchema } from './user.schema' +import { describe, it, expect } from 'bun:test' describe('validate alias', () => { - beforeEach(() => { - jest.clearAllMocks() - }) - describe('alias contains invalid characters', () => { it('throws and logs an error', () => { expect(() => aliasSchema.parse('Bibi.Bloxberg')).toThrowError(expect.objectContaining( @@ -104,3 +95,35 @@ describe('validate alias', () => { }) }) }) + +describe('validate first name', () => { + describe('first name contains invalid characters', () => { + it('throws and logs an error', () => { + expect(() => firstNameSchema.parse('')).toThrowError(expect.objectContaining( + expect.arrayContaining([ + expect.objectContaining({ + origin: 'string', + code: 'invalid_format', + format: 'regex', + message: 'Invalid characters in first name', + path: [], + }) + ]) + )) + }) + }) + it('use greek symbols', () => { + expect(() => firstNameSchema.parse('Αλέξανδρος')).not.toThrowError() + }) + it('use korean symbols', () => { + expect(() => firstNameSchema.parse('김민수')).not.toThrowError() + }) + // TODO: use min length depending of language, because in asiatic languages first and/or last names can have only one character + it.skip('use japanese symbols', () => { + expect(() => firstNameSchema.parse('田中')).not.toThrowError() + }) + // TODO: fix this + it.skip('use chinese symbols', () => { + expect(() => firstNameSchema.parse('张三')).not.toThrowError() + }) +}) diff --git a/shared/src/schema/user.schema.ts b/shared/src/schema/user.schema.ts index 90897a637..494ee03b6 100644 --- a/shared/src/schema/user.schema.ts +++ b/shared/src/schema/user.schema.ts @@ -1,6 +1,11 @@ import { string } from 'zod' export const VALID_ALIAS_REGEX = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/ +// \p{L} = a character from every alphabet (latin, greek, cyrillic, etc.) +// first a character or ' is expected +// then all without the last a character, space, apostrophe or hyphen is expected +// last a character is expected +export const VALID_NAME_REGEX = /^[\p{L}'][ \p{L}'-_]*[\p{L}]$/u const RESERVED_ALIAS = [ 'admin', @@ -26,3 +31,15 @@ export const aliasSchema = string() .refine((val) => !RESERVED_ALIAS.includes(val.toLowerCase()), { message: 'Given alias is not allowed', }) + +// TODO: use this schemas in backend, think about case which currently not fullfil the regex +// (some user start there name with : ) +export const firstNameSchema = string() + .min(3, 'First name is too short') + .max(255, 'First name is too long') + .regex(VALID_NAME_REGEX) + +export const lastNameSchema = string() + .min(2, 'Last name is too short') + .max(255, 'Last name is too long') + .regex(VALID_NAME_REGEX) \ No newline at end of file