196 lines
5.5 KiB
JavaScript
196 lines
5.5 KiB
JavaScript
/**
|
|
* Validators - Input validation utilities
|
|
*/
|
|
|
|
/**
|
|
* Validate if value is a number
|
|
* @param {any} value - Value to validate
|
|
* @returns {boolean} True if valid number
|
|
*/
|
|
export function isValidNumber(value) {
|
|
return !isNaN(parseFloat(value)) && isFinite(value);
|
|
}
|
|
|
|
/**
|
|
* Validate if value is within range
|
|
* @param {number} value - Value to validate
|
|
* @param {number} min - Minimum value
|
|
* @param {number} max - Maximum value
|
|
* @returns {boolean} True if within range
|
|
*/
|
|
export function isInRange(value, min, max) {
|
|
if (!isValidNumber(value)) return false;
|
|
const num = parseFloat(value);
|
|
return num >= min && num <= max;
|
|
}
|
|
|
|
/**
|
|
* Validate if value is positive
|
|
* @param {number} value - Value to validate
|
|
* @returns {boolean} True if positive
|
|
*/
|
|
export function isPositive(value) {
|
|
return isValidNumber(value) && parseFloat(value) > 0;
|
|
}
|
|
|
|
/**
|
|
* Validate if value is non-negative
|
|
* @param {number} value - Value to validate
|
|
* @returns {boolean} True if non-negative
|
|
*/
|
|
export function isNonNegative(value) {
|
|
return isValidNumber(value) && parseFloat(value) >= 0;
|
|
}
|
|
|
|
/**
|
|
* Validate email format
|
|
* @param {string} email - Email to validate
|
|
* @returns {boolean} True if valid email
|
|
*/
|
|
export function isValidEmail(email) {
|
|
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return regex.test(email);
|
|
}
|
|
|
|
/**
|
|
* Validate if string is not empty
|
|
* @param {string} value - String to validate
|
|
* @returns {boolean} True if not empty
|
|
*/
|
|
export function isNotEmpty(value) {
|
|
return value !== null && value !== undefined && value.toString().trim().length > 0;
|
|
}
|
|
|
|
/**
|
|
* Validate CEV value (typical range)
|
|
* @param {number} cev - CEV value
|
|
* @returns {object} Validation result with message
|
|
*/
|
|
export function validateCEV(cev) {
|
|
if (!isValidNumber(cev)) {
|
|
return { valid: false, message: 'CEV deve ser um número válido' };
|
|
}
|
|
|
|
const value = parseFloat(cev);
|
|
|
|
if (value < 0) {
|
|
return { valid: false, message: 'CEV não pode ser negativo' };
|
|
}
|
|
|
|
if (value > 1.0) {
|
|
return { valid: false, message: 'CEV muito alto (>1.0). Verifique os valores.' };
|
|
}
|
|
|
|
if (value > 0.65) {
|
|
return { valid: true, message: 'Atenção: CEV muito alto (>0.65). Soldabilidade difícil.' };
|
|
}
|
|
|
|
return { valid: true, message: '' };
|
|
}
|
|
|
|
/**
|
|
* Validate temperature value
|
|
* @param {number} temp - Temperature in Celsius
|
|
* @param {number} min - Minimum temperature
|
|
* @param {number} max - Maximum temperature
|
|
* @returns {object} Validation result
|
|
*/
|
|
export function validateTemperature(temp, min = -50, max = 500) {
|
|
if (!isValidNumber(temp)) {
|
|
return { valid: false, message: 'Temperatura deve ser um número válido' };
|
|
}
|
|
|
|
const value = parseFloat(temp);
|
|
|
|
if (value < min) {
|
|
return { valid: false, message: `Temperatura muito baixa (mínimo: ${min}°C)` };
|
|
}
|
|
|
|
if (value > max) {
|
|
return { valid: false, message: `Temperatura muito alta (máximo: ${max}°C)` };
|
|
}
|
|
|
|
return { valid: true, message: '' };
|
|
}
|
|
|
|
/**
|
|
* Validate percentage value
|
|
* @param {number} value - Percentage value
|
|
* @param {boolean} isDecimal - If true, expects 0-1, else 0-100
|
|
* @returns {object} Validation result
|
|
*/
|
|
export function validatePercentage(value, isDecimal = false) {
|
|
if (!isValidNumber(value)) {
|
|
return { valid: false, message: 'Porcentagem deve ser um número válido' };
|
|
}
|
|
|
|
const num = parseFloat(value);
|
|
const max = isDecimal ? 1 : 100;
|
|
|
|
if (num < 0) {
|
|
return { valid: false, message: 'Porcentagem não pode ser negativa' };
|
|
}
|
|
|
|
if (num > max) {
|
|
return { valid: false, message: `Porcentagem não pode exceder ${max}${isDecimal ? '' : '%'}` };
|
|
}
|
|
|
|
return { valid: true, message: '' };
|
|
}
|
|
|
|
/**
|
|
* Validate form inputs
|
|
* @param {object} inputs - Object with input values
|
|
* @param {object} rules - Validation rules
|
|
* @returns {object} Validation result with errors
|
|
*/
|
|
export function validateForm(inputs, rules) {
|
|
const errors = {};
|
|
let isValid = true;
|
|
|
|
for (const [field, value] of Object.entries(inputs)) {
|
|
const rule = rules[field];
|
|
if (!rule) continue;
|
|
|
|
// Required check
|
|
if (rule.required && !isNotEmpty(value)) {
|
|
errors[field] = 'Campo obrigatório';
|
|
isValid = false;
|
|
continue;
|
|
}
|
|
|
|
// Type check
|
|
if (rule.type === 'number' && !isValidNumber(value)) {
|
|
errors[field] = 'Deve ser um número válido';
|
|
isValid = false;
|
|
continue;
|
|
}
|
|
|
|
// Range check
|
|
if (rule.min !== undefined || rule.max !== undefined) {
|
|
const num = parseFloat(value);
|
|
if (rule.min !== undefined && num < rule.min) {
|
|
errors[field] = `Valor mínimo: ${rule.min}`;
|
|
isValid = false;
|
|
continue;
|
|
}
|
|
if (rule.max !== undefined && num > rule.max) {
|
|
errors[field] = `Valor máximo: ${rule.max}`;
|
|
isValid = false;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Custom validator
|
|
if (rule.validator) {
|
|
const result = rule.validator(value);
|
|
if (!result.valid) {
|
|
errors[field] = result.message;
|
|
isValid = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return { isValid, errors };
|
|
}
|