Skip to content

Commit

Permalink
Enhanced Member Expression Check (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 authored Mar 26, 2023
1 parent 39afc1f commit 89564f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.26.3",
"version": "0.26.4",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
58 changes: 29 additions & 29 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,38 @@ namespace Character {
export function DollarSign(code: number) {
return code === 36
}
export function Underscore(code: number) {
export function IsUnderscore(code: number) {
return code === 95
}
export function Numeric(code: number) {
export function IsAlpha(code: number) {
return (code >= 64 && code <= 90) || (code >= 97 && code <= 122)
}
export function IsNumeric(code: number) {
return code >= 48 && code <= 57
}
export function Alpha(code: number) {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
}
// -------------------------------------------------------------------
// MemberExpression
// -------------------------------------------------------------------
namespace MemberExpression {
function IsFirstCharacterNumeric(value: string) {
if (value.length === 0) return false
return Character.IsNumeric(value.charCodeAt(0))
}
function IsAccessor(value: string) {
if (IsFirstCharacterNumeric(value)) return false
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i)
const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code)
if (!check) return false
}
return true
}
function EscapeHyphen(key: string) {
return key.replace(/'/g, "\\'")
}
export function Encode(object: string, key: string) {
return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`
}
}
// -------------------------------------------------------------------
Expand All @@ -78,7 +102,7 @@ namespace Identifier {
const buffer: string[] = []
for (let i = 0; i < $id.length; i++) {
const code = $id.charCodeAt(i)
if (Character.Numeric(code) || Character.Alpha(code)) {
if (Character.IsNumeric(code) || Character.IsAlpha(code)) {
buffer.push($id.charAt(i))
} else {
buffer.push(`_${code}_`)
Expand All @@ -88,30 +112,6 @@ namespace Identifier {
}
}
// -------------------------------------------------------------------
// MemberExpression
// -------------------------------------------------------------------
export namespace MemberExpression {
function Check(propertyName: string) {
if (propertyName.length === 0) return false
{
const code = propertyName.charCodeAt(0)
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
return false
}
}
for (let i = 1; i < propertyName.length; i++) {
const code = propertyName.charCodeAt(i)
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
return false
}
}
return true
}
export function Encode(object: string, key: string) {
return !Check(key) ? `${object}['${key}']` : `${object}.${key}`
}
}
// -------------------------------------------------------------------
// TypeCompiler
// -------------------------------------------------------------------
export class TypeCompilerUnknownTypeError extends Error {
Expand Down
4 changes: 4 additions & 0 deletions test/runtime/compiler/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ describe('type/compiler/Object', () => {
'0-leading': Type.Literal(2),
'$-leading': Type.Literal(3),
'!@#$%^&*(': Type.Literal(4),
'node-mirror:release': Type.Literal(5), // issue: 353
"a'a": Type.Literal(6),
})
Ok(T, {
'with-hyphen': 1,
'0-leading': 2,
'$-leading': 3,
'!@#$%^&*(': 4,
'node-mirror:release': 5,
"a'a": 6,
})
})
it('Should validate schema additional properties of string', () => {
Expand Down

0 comments on commit 89564f1

Please sign in to comment.