credix
Version:
Official SDK for Credix Credit Management System
132 lines • 4.4 kB
JavaScript
/** biome-ignore-all lint/complexity/noStaticOnlyClass: static warningは無視するように */
import { ValidationError } from '../errors/index.js';
/**
* Validation utilities for SDK inputs
*/
export class Validator {
/**
* Validates that a value is not null or undefined
*/
static required(value, fieldName) {
if (value === null || value === undefined) {
throw new ValidationError(`${fieldName} is required`, fieldName);
}
return value;
}
/**
* Validates that a string is not empty
*/
static notEmpty(value, fieldName) {
const val = Validator.required(value, fieldName);
if (val.trim().length === 0) {
throw new ValidationError(`${fieldName} cannot be empty`, fieldName, value);
}
return val;
}
/**
* Validates that a value is a positive number
*/
static positiveNumber(value, fieldName) {
const val = Validator.required(value, fieldName);
if (val <= 0) {
throw new ValidationError(`${fieldName} must be a positive number`, fieldName, value);
}
return val;
}
/**
* Validates that a value is a non-negative number
*/
static nonNegativeNumber(value, fieldName) {
const val = Validator.required(value, fieldName);
if (val < 0) {
throw new ValidationError(`${fieldName} must be non-negative`, fieldName, value);
}
return val;
}
/**
* Validates that a value is within a range
*/
static inRange(value, fieldName, min, max) {
const val = Validator.required(value, fieldName);
if (val < min || val > max) {
throw new ValidationError(`${fieldName} must be between ${min} and ${max}`, fieldName, value);
}
return val;
}
/**
* Validates an email address
*/
static email(value, fieldName) {
const val = Validator.notEmpty(value, fieldName);
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(val)) {
throw new ValidationError(`${fieldName} must be a valid email address`, fieldName, value);
}
return val;
}
/**
* Validates a UUID
*/
static uuid(value, fieldName) {
const val = Validator.notEmpty(value, fieldName);
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(val)) {
throw new ValidationError(`${fieldName} must be a valid UUID`, fieldName, value);
}
return val;
}
/**
* Validates an ISO date string
*/
static isoDate(value, fieldName) {
const val = Validator.notEmpty(value, fieldName);
const date = new Date(val);
if (Number.isNaN(date.getTime())) {
throw new ValidationError(`${fieldName} must be a valid ISO date string`, fieldName, value);
}
return val;
}
/**
* Checks if a string is ISO 8601 format (for validation only)
*/
static isISO8601(value) {
if (!value) {
return false;
}
const date = new Date(value);
return !Number.isNaN(date.getTime()) && value.includes('T');
}
/**
* Validates an enum value
*/
static enum(value, fieldName, validValues) {
const val = Validator.required(value, fieldName);
if (!validValues.includes(val)) {
throw new ValidationError(`${fieldName} must be one of: ${validValues.join(', ')}`, fieldName, value);
}
return val;
}
/**
* Validates an array is not empty
*/
static notEmptyArray(value, fieldName) {
const val = Validator.required(value, fieldName);
if (val.length === 0) {
throw new ValidationError(`${fieldName} cannot be an empty array`, fieldName, value);
}
return val;
}
/**
* Validates object has required properties
*/
static hasProperties(value, fieldName, requiredProps) {
const val = Validator.required(value, fieldName);
for (const prop of requiredProps) {
if (!(prop in val)) {
throw new ValidationError(`${fieldName} must have property: ${String(prop)}`, fieldName, value);
}
}
return val;
}
}
//# sourceMappingURL=validator.js.map