json-api-nestjs
Version:
JsonApi Plugin for NestJs
54 lines • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.elementOfArrayMustBe = exports.stringMustBe = exports.arrayItemStringLongerThan = exports.stringLongerThan = exports.oneOf = exports.getValidationErrorForStrict = exports.uniqueArray = exports.nonEmptyObject = void 0;
exports.guardIsKeyOfObject = guardIsKeyOfObject;
const types_1 = require("../types");
const nonEmptyObject = () => (object) => !!(typeof object === 'object' && object && Object.keys(object).length > 0);
exports.nonEmptyObject = nonEmptyObject;
const uniqueArray = () => (e) => new Set(e).size === e.length;
exports.uniqueArray = uniqueArray;
const getValidationErrorForStrict = (props, name) => `Validation error: ${name} should be have only props: ["${props.join('","')}"]`;
exports.getValidationErrorForStrict = getValidationErrorForStrict;
const oneOf = (keys) => (val) => {
if (!val)
return false;
for (const k of keys) {
if (val[k] !== undefined)
return true;
}
return false;
};
exports.oneOf = oneOf;
const stringLongerThan = (length = 0) => (str) => str.length > length;
exports.stringLongerThan = stringLongerThan;
const arrayItemStringLongerThan = (length = 0) => (array) => {
const checkFunction = (0, exports.stringLongerThan)(length);
return !array.some((i) => i !== null && !checkFunction(i));
};
exports.arrayItemStringLongerThan = arrayItemStringLongerThan;
const stringMustBe = (type = types_1.TypeField.string) => (inputString) => {
if (inputString === null)
return true;
switch (type) {
case types_1.TypeField.boolean:
return inputString === 'true' || inputString === 'false';
case types_1.TypeField.number:
return !isNaN(+inputString);
case types_1.TypeField.date:
return new Date(inputString).toString() !== 'Invalid Date';
default:
return true;
}
};
exports.stringMustBe = stringMustBe;
const elementOfArrayMustBe = (type = types_1.TypeField.string) => (inputArray) => {
const checkFunc = (0, exports.stringMustBe)(type);
return !inputArray.some((i) => !checkFunc(`${i}`));
};
exports.elementOfArrayMustBe = elementOfArrayMustBe;
function guardIsKeyOfObject(object, key) {
if (typeof object === 'object' && object !== null && key in object)
return void 0;
throw new Error('Type guard error');
}
//# sourceMappingURL=zod-utils.js.map