Skip to content

Commit 4f06583

Browse files
committed
🎉 feat: add schema.trusted for string
1 parent 193eb7f commit 4f06583

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
# 0.1.5 - 22 Apr 2025
2+
Improvement:
3+
- add `schema.trusted` for string
4+
- improve string placement when using `t.String({ trusted: true })` and `sanitize: manual`
5+
16
# 0.1.4 - 27 Mar 2025
27
Improvement:
3-
- Improve array performance by avoiding unnecessary closure reference
8+
- improve array performance by avoiding unnecessary closure reference
49

510
# 0.1.3 - 14 Mar 2025
611
Bug fix:

benchmarks/message.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { t } from 'elysia'
2+
import { benchmark } from './utils'
3+
4+
benchmark(
5+
t.Object({
6+
message: t.String({
7+
trusted: true
8+
})
9+
}),
10+
{ message: 'Hello, World!' as const }
11+
)

example/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import { t } from 'elysia'
22
import { createAccelerator } from '../src/index'
33

44
const shape = t.Object({
5-
name: t.String(),
6-
playing: t.Nullable(t.Integer({ default: 1 }))
5+
message: t.String({
6+
trusted: true
7+
})
78
})
89

9-
console.log(t.Optional(t.String()))
10-
1110
const value = {
12-
name: 'saltyaom',
13-
playing: null
11+
message: 'a'
1412
} satisfies typeof shape.static
1513

1614
const mirror = createAccelerator(shape)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-accelerator",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Speed up JSON stringification by providing OpenAPI/TypeBox model",
55
"license": "MIT",
66
"scripts": {
@@ -11,7 +11,7 @@
1111
"release": "npm run build && npm run test && npm publish --access public"
1212
},
1313
"peerDependencies": {
14-
"@sinclair/typebox": "^0.34.31"
14+
"@sinclair/typebox": ">= 0.34.0"
1515
},
1616
"peerDependenciesMeta": {
1717
"@sinclair/typebox": {

src/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ const isInteger = (schema: TAnySchema) => {
3636
continue
3737
}
3838

39-
if (!hasNumberType && (type.type === 'number' || type.type === 'integer')) {
39+
if (
40+
!hasNumberType &&
41+
(type.type === 'number' || type.type === 'integer')
42+
) {
4043
hasNumberType = true
4144
continue
4245
}
@@ -257,7 +260,7 @@ const accelerate = (
257260

258261
switch (schema.type) {
259262
case 'string':
260-
instruction.hasString = true
263+
if (!schema.const && !schema.trusted) instruction.hasString = true
261264

262265
// string operation would be repeated multiple time
263266
// it's fine to optimize it to the most optimized way
@@ -272,10 +275,19 @@ const accelerate = (
272275
// this handle the case where the string contains double quotes
273276
// As slice(1,-1) is use several compute and would be called multiple times
274277
// it's not ideal to slice(1, -1) of JSON.stringify
275-
if (nullableCondition)
278+
if (nullableCondition) {
279+
if (schema.trusted)
280+
sanitize = (v: string) =>
281+
`\`"$\{${SANITIZE['manual'](v)}}"\``
282+
276283
v = `\${${nullableCondition}?${schema.const !== undefined ? `'${JSON.stringify(schema.const)}'` : schema.default !== undefined ? `'${JSON.stringify(schema.default)}'` : `'null'`}:${sanitize(property)}}`
277-
else
278-
v = `${schema.const !== undefined ? `${JSON.stringify(schema.const)}` : `\${${sanitize(property)}}`}`
284+
} else {
285+
if (schema.const !== undefined)
286+
v = JSON.stringify(schema.const)
287+
else if (schema.trusted)
288+
v = `"\${${SANITIZE['manual'](property)}}"`
289+
else v = `\${${sanitize(property)}}`
290+
}
279291
} else {
280292
// In this case quote is handle outside to improve performance
281293
if (nullableCondition)

0 commit comments

Comments
 (0)