veffect
Version:
powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha
216 lines (215 loc) • 9.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.number = number;
/**
* Number schema implementation
*/
const E = __importStar(require("../internal/effect"));
const validator_1 = require("../validator");
const errors_1 = require("../errors");
/**
* Create a number schema
*/
function number() {
const validations = [];
let errorMessage = undefined;
const schema = {
_tag: 'NumberSchema',
min: (min, message) => {
validations.push((input, options) => input >= min
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be at least ${min}`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
max: (max, message) => {
validations.push((input, options) => input <= max
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be at most ${max}`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
integer: (message) => {
validations.push((input, options) => Number.isInteger(input)
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be an integer`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
positive: (message) => {
validations.push((input, options) => input > 0
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be positive`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
negative: (message) => {
validations.push((input, options) => input < 0
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be negative`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
nonnegative: (message) => {
validations.push((input, options) => input >= 0
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be non-negative`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
nonpositive: (message) => {
validations.push((input, options) => input <= 0
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be non-positive`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
multipleOf: (value, message) => {
validations.push((input, options) => {
// Handle floating point precision issues
const remainder = (input / value) % 1;
const isMultiple = remainder < Number.EPSILON || remainder > 1 - Number.EPSILON;
return isMultiple
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be a multiple of ${value}`, options === null || options === void 0 ? void 0 : options.path));
});
return schema;
},
finite: (message) => {
validations.push((input, options) => Number.isFinite(input)
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be finite`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
safe: (message) => {
validations.push((input, options) => Number.isSafeInteger(input) || (Number.isFinite(input) && !Number.isInteger(input))
? E.succeed(input)
: E.fail(new errors_1.NumberValidationError(message || `Number must be a safe number`, options === null || options === void 0 ? void 0 : options.path)));
return schema;
},
step: (step, message) => {
return schema.multipleOf(step, message || `Number must be a multiple of ${step}`);
},
port: (message) => {
return schema
.integer(message || 'Port must be an integer')
.min(1, message || 'Port must be at least 1')
.max(65535, message || 'Port must be at most 65535');
},
// Refinement implementation
refine: (refinement, message) => {
validations.push((input, options) => refinement(input)
? E.succeed(input)
: E.fail({
_tag: 'RefinementValidationError',
message: typeof message === 'function'
? message(input)
: message || 'Failed refinement',
path: options === null || options === void 0 ? void 0 : options.path
}));
return schema;
},
// Predicate implementation (alias for refine)
predicate: (predicate, message) => {
return schema.refine(predicate, message);
},
// Transform implementation
transform: (transformer) => {
return {
_tag: 'TransformedSchema',
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
return E.pipe(schema.toValidator().validate(input, options), E.map(value => transformer(value)));
})
};
},
// Default value implementation
default: (defaultValue) => {
return {
...schema,
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
if (input === undefined) {
const value = typeof defaultValue === 'function'
? defaultValue()
: defaultValue;
return E.succeed(value);
}
return schema.toValidator().validate(input, options);
})
};
},
// Nullable implementation
nullable: () => {
return {
...schema,
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
if (input === null) {
return E.succeed(null);
}
return schema.toValidator().validate(input, options);
})
};
},
// Optional implementation
optional: () => {
return {
_tag: 'OptionalSchema',
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
if (input === undefined) {
return E.succeed(undefined);
}
return schema.toValidator().validate(input, options);
})
};
},
// Nullish implementation
nullish: () => {
return {
_tag: 'NullishSchema',
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
if (input === null || input === undefined) {
return E.succeed(input);
}
return schema.toValidator().validate(input, options);
})
};
},
// Custom error message
error: (message) => {
errorMessage = message;
return schema;
},
toValidator: () => (0, validator_1.createEffectValidator)((input, options) => {
if (typeof input !== 'number' || Number.isNaN(input)) {
return E.fail(new errors_1.TypeValidationError(errorMessage || 'Value must be a number', 'number', typeof input, options === null || options === void 0 ? void 0 : options.path));
}
return validations.reduce((acc, validation) => E.flatMap(acc, val => validation(val, options)), E.succeed(input));
})
};
return schema;
}