@lovebowls/leaguejs
Version:
A framework-agnostic JavaScript library for managing leagues, teams, and matches
87 lines (74 loc) • 2.67 kB
JavaScript
/**
* Validation utilities for the leagueJS
*/
export function validateLeague(data) {
const errors = [];
if (!data.name) {
errors.push('League name is required');
}
if (data.settings) {
if (typeof data.settings.pointsForWin !== 'undefined') {
if (typeof data.settings.pointsForWin !== 'number' || data.settings.pointsForWin < 0) {
errors.push('Invalid points settings');
}
}
if (typeof data.settings.pointsForDraw !== 'undefined') {
if (typeof data.settings.pointsForDraw !== 'number' || data.settings.pointsForDraw < 0) {
errors.push('Invalid points settings');
}
}
if (typeof data.settings.pointsForLoss !== 'undefined') {
if (typeof data.settings.pointsForLoss !== 'number' || data.settings.pointsForLoss < 0) {
errors.push('Invalid points settings');
}
}
// Validate timesTeamsPlayOther
if (typeof data.settings.timesTeamsPlayOther !== 'undefined') {
if (typeof data.settings.timesTeamsPlayOther !== 'number' ||
data.settings.timesTeamsPlayOther < 1 ||
data.settings.timesTeamsPlayOther > 10) {
errors.push('timesTeamsPlayOther must be an integer between 1 and 10');
}
}
}
return {
isValid: errors.length === 0,
errors
};
}
export function validateTeam(data) {
const errors = [];
if (!data._id || !data._id.trim()) {
errors.push('Team ID is required');
}
return {
isValid: errors.length === 0,
errors
};
}
export function validateMatch(data) {
const errors = [];
if (!data.homeTeam || !data.homeTeam._id) {
errors.push('Home team is required');
}
if (!data.awayTeam || !data.awayTeam._id) {
errors.push('Away team is required');
}
if (data.date && !(data.date instanceof Date) && isNaN(new Date(data.date).getTime())) {
errors.push('Invalid date format');
}
// Validate result scores if result object exists
if (data.result) {
if (typeof data.result.homeScore !== 'number' || data.result.homeScore < 0) {
errors.push('Home score must be a non-negative number');
}
if (typeof data.result.awayScore !== 'number' || data.result.awayScore < 0) {
errors.push('Away score must be a non-negative number');
}
// Optionally, could add validation for rinkScores structure here if needed
}
return {
isValid: errors.length === 0,
errors
};
}