Skip to content

Commit

Permalink
fix: ensure we check names using the full UTF-8 character set.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjdhjd committed Jul 4, 2024
1 parent 853171f commit b909b03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/lib/util/checkName.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { CharacteristicValue, Nullable } from "../../types";

/**
* Checks that supplied field meets Apple HomeKit naming rules
* https://developer.apple.com/design/human-interface-guidelines/homekit#Help-people-choose-useful-names
* @private Private API
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function checkName(displayName: string, name: string, value: any): void {
const validHK = /^[a-zA-Z0-9\s'-.]+$/; // Ensure only letter, numbers, apostrophe, or dash
const startWith = /^[a-zA-Z0-9]/; // Ensure only letters or numbers are at the beginning of string
const endWith = /[a-zA-Z0-9]$/; // Ensure only letters or numbers are at the end of string
export function checkName(displayName: string, name: string, value: Nullable<CharacteristicValue>): void {

if (!validHK.test(value) || !startWith.test(value) || !endWith.test(value)) {
// Ensure the string starts and ends with a Unicode letter or number and allow any combination of letters, numbers, spaces, and apostrophes in the middle.
if (typeof value === "string" && value.length && !(new RegExp(/^[\p{L}\p{N}][\p{L}\p{N} ']*[\p{L}\p{N}]$/u)).test(value)) {
console.warn("HAP-NodeJS WARNING: The accessory '" + displayName + "' is getting published with the characteristic '" +
name + "'" + " not following HomeKit naming rules ('" + value + "'). " +
"Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. " +
"This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
}
}
}
24 changes: 10 additions & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"downlevelIteration": true,
"importHelpers": true,
"lib": [
"es2015",
"es2016",
"es2017",
"es2018"
],
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"lib": ["ES2022"],
"module": "CommonJS",
"moduleResolution": "node",
"preserveConstEnums": true, // do not remove this option!
"outDir": "./dist",
"rootDir": "./src",
"sourceMap": true,
"strict": true,
"target": "ES2022",
"useUnknownInCatchVariables": false
},
"include": [
Expand Down

0 comments on commit b909b03

Please sign in to comment.