Skip to content

Non-Empty String Validator

The nonEmptyString validator ensures that a string value is not empty and contains meaningful content (after trimming whitespace).

validators.nonEmptyString: StateValidator
import { withLocalState, validators } from 'phaser-hooks';
export class GameScene extends Phaser.Scene {
create() {
// Player name must not be empty
const playerName = withLocalState(this, 'playerName', 'Player', {
validator: validators.nonEmptyString
});
// Game title must not be empty
const gameTitle = withLocalState(this, 'gameTitle', 'My Game', {
validator: validators.nonEmptyString
});
// Save file name must not be empty
const saveFileName = withLocalState(this, 'saveFile', 'save1', {
validator: validators.nonEmptyString
});
}
}

Always wrap state updates in try/catch blocks when using validators:

const playerName = withLocalState(this, 'playerName', 'Player', {
validator: validators.nonEmptyString
});
function updatePlayerName(newName: string) {
try {
playerName.set(newName);
console.log(`Player name updated to: ${newName}`);
} catch (error) {
console.error('Invalid player name:', error.message);
// Handle the error - maybe use a default name
playerName.set('Player');
}
}
function handleUserInput(input: string) {
try {
playerName.set(input);
} catch (error) {
console.error('Invalid input:', error.message);
// Show error message to user
this.showErrorMessage('Name cannot be empty');
}
}
export class UserInputManager {
private playerName: HookState<string>;
private chatMessage: HookState<string>;
constructor(scene: Phaser.Scene) {
this.playerName = withLocalState(scene, 'playerName', '', {
validator: validators.nonEmptyString
});
this.chatMessage = withLocalState(scene, 'chatMessage', '', {
validator: validators.nonEmptyString
});
}
setName(name: string) {
try {
this.playerName.set(name.trim());
return true;
} catch (error) {
console.error('Invalid name:', error.message);
return false;
}
}
sendMessage(message: string) {
try {
this.chatMessage.set(message.trim());
// Process the message
this.processChatMessage(this.chatMessage.get());
return true;
} catch (error) {
console.error('Invalid message:', error.message);
return false;
}
}
}
export class GameConfig {
private serverUrl: HookState<string>;
private apiKey: HookState<string>;
private language: HookState<string>;
constructor(scene: Phaser.Scene) {
this.serverUrl = withGlobalState(scene, 'serverUrl', 'https://api.game.com', {
validator: validators.nonEmptyString
});
this.apiKey = withGlobalState(scene, 'apiKey', '', {
validator: validators.nonEmptyString
});
this.language = withGlobalState(scene, 'language', 'en', {
validator: validators.nonEmptyString
});
}
updateServerUrl(url: string) {
try {
this.serverUrl.set(url.trim());
console.log('Server URL updated');
} catch (error) {
console.error('Invalid server URL:', error.message);
throw new Error('Server URL cannot be empty');
}
}
}
export class SaveGameManager {
private saveName: HookState<string>;
private saveDescription: HookState<string>;
constructor(scene: Phaser.Scene) {
this.saveName = withLocalState(scene, 'saveName', '', {
validator: validators.nonEmptyString
});
this.saveDescription = withLocalState(scene, 'saveDescription', '', {
validator: validators.nonEmptyString
});
}
createSave(name: string, description: string) {
try {
this.saveName.set(name.trim());
this.saveDescription.set(description.trim());
// Proceed with save creation
this.performSave();
return true;
} catch (error) {
console.error('Save validation failed:', error.message);
return false;
}
}
}

The nonEmptyString validator returns specific error messages:

  • "Value must be a non-empty string" - When the value is not a string or is empty after trimming
  • The validator trims whitespace before checking if the string is empty
  • Empty strings ("") and whitespace-only strings (" ") will fail validation
  • The validator only checks for non-empty strings, not string length limits