Non-Empty String Validator
Non-Empty String Validator
Section titled “Non-Empty String Validator”The nonEmptyString validator ensures that a string value is not empty and contains meaningful content (after trimming whitespace).
Syntax
Section titled “Syntax”validators.nonEmptyString: StateValidatorBasic Usage
Section titled “Basic Usage”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 }); }}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 }); }}Error Handling
Section titled “Error Handling”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'); }}const playerName = withLocalState(this, 'playerName', 'Player', { validator: validators.nonEmptyString});
function updatePlayerName(newName) { 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) { try { playerName.set(input); } catch (error) { console.error('Invalid input:', error.message); // Show error message to user this.showErrorMessage('Name cannot be empty'); }}Common Use Cases
Section titled “Common Use Cases”User Input Validation
Section titled “User Input Validation”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 UserInputManager { constructor(scene) { this.playerName = withLocalState(scene, 'playerName', '', { validator: validators.nonEmptyString });
this.chatMessage = withLocalState(scene, 'chatMessage', '', { validator: validators.nonEmptyString }); }
setName(name) { try { this.playerName.set(name.trim()); return true; } catch (error) { console.error('Invalid name:', error.message); return false; } }
sendMessage(message) { 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; } }}Game Configuration
Section titled “Game Configuration”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 GameConfig { constructor(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) { 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'); } }}Save Game Data
Section titled “Save Game Data”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; } }}export class SaveGameManager { constructor(scene) { this.saveName = withLocalState(scene, 'saveName', '', { validator: validators.nonEmptyString });
this.saveDescription = withLocalState(scene, 'saveDescription', '', { validator: validators.nonEmptyString }); }
createSave(name, description) { 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; } }}Error Messages
Section titled “Error Messages”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
Important Notes
Section titled “Important Notes”- 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
Next Steps
Section titled “Next Steps”- Array Length Validator - Learn about array validation
- One Of Validator - Learn about enum validation
- Creating Custom Validators - Learn to create your own validators