mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
more schemas
This commit is contained in:
parent
9fc6a63bea
commit
85fa997648
@ -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('<script>//malicious code</script>')).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()
|
||||
})
|
||||
})
|
||||
|
||||
@ -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)
|
||||
Loading…
x
Reference in New Issue
Block a user