Skip to content

Commit

Permalink
Add additional string formats directly from ajv-formats
Browse files Browse the repository at this point in the history
  • Loading branch information
FDiskas committed Apr 1, 2024
1 parent c474941 commit 5f8af50
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"typescript": "^5.4.2"
},
"peerDependencies": {
"ajv-formats": "^3.0.1",
"@sinclair/typebox": ">= 0.31.0",
"openapi-types": ">= 12.0.0",
"typescript": ">= 5.0.0"
Expand Down
17 changes: 7 additions & 10 deletions src/type-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {

import { type TypeCheck } from '@sinclair/typebox/compiler'
import { Value } from '@sinclair/typebox/value'
import { fullFormats } from 'ajv-formats/dist/formats'

import type { CookieOptions } from './cookies'
import { ValidationError } from './error'
Expand All @@ -30,17 +31,13 @@ const isShortenDate =
/^(?:(?:(?:(?:0?[1-9]|[12][0-9]|3[01])[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:19|20)\d{2})|(?:(?:19|20)\d{2}[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:0?[1-9]|[12][0-9]|3[01]))))(?:\s(?:1[012]|0?[1-9]):[0-5][0-9](?::[0-5][0-9])?(?:\s[AP]M)?)?$/

try {
TypeSystem.Format('email', (value) =>
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]+(?:-[a-z0-9]+)*$/i.test(
value
)
)

TypeSystem.Format('uuid', (value) =>
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
value
)
)
Object.entries(fullFormats).forEach(formatEntry => {
const [formatName, formatValue] = formatEntry;
if (formatValue instanceof RegExp) {
TypeSystem.Format(formatName, (value) => formatValue.test(value))
}
})

TypeSystem.Format('date', (value) => {
// Remove quote from stringified date
Expand Down
65 changes: 65 additions & 0 deletions test/type-system/string-format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Elysia, { t } from '../../src'
import { describe, expect, it } from 'bun:test'
import { Value } from '@sinclair/typebox/value'
import { TBoolean, TString, TypeBoxError } from '@sinclair/typebox'
import { req } from '../utils'

describe('TypeSystem - ObjectString', () => {
it('Format email', async () => {
const testString = "[email protected]";
const app = new Elysia().get(
'/',
({ query }) => query,
{
query: t.Object({
email: t.String({
format: 'email'
})
})
}
)

const res1 = await app.handle(req(`/?email=${testString}`))
expect(res1.status).toBe(200);

expect(await (res1).json()).toEqual({ email: testString })
})
it('Format hostname', async () => {
const testString = "www";
const app = new Elysia().get(
'/',
({ query }) => query,
{
query: t.Object({
host: t.String({
format: 'hostname'
})
})
}
)

const res1 = await app.handle(req(`/?host=${testString}`))
expect(res1.status).toBe(200);

expect(await (res1).json()).toEqual({ host: testString })
})
it('Format date', async () => {
const testString = "2024-01-01";
const app = new Elysia().get(
'/',
({ query }) => query,
{
query: t.Object({
date: t.String({
format: 'date'
})
})
}
)

const res1 = await app.handle(req(`/?date=${testString}`))
expect(res1.status).toBe(200);

expect(await (res1).json()).toEqual({ date: testString })
})
})

0 comments on commit 5f8af50

Please sign in to comment.