next-server-action-validation
Version:
[](https://www.npmjs.com/package/next-server-action-validation) [](https://opensource.org/licenses/MIT)
32 lines (31 loc) • 1.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withValidation = void 0;
/**
* Validates the data with the given zod schema, and if it is valid, runs the given function.
* @param schema The zod schema to validate the data with
* @param func The function to run if the data is valid
* @returns The function to run if the data is valid, or a function that returns a promise of a validation error
*/
function withValidation(schema, func) {
return ((data) => __awaiter(this, void 0, void 0, function* () {
const zodValidationResult = schema.safeParse(data);
if (!zodValidationResult.success) {
return {
isValidationError: true,
errors: zodValidationResult.error.errors
};
}
return func(zodValidationResult.data);
}));
}
exports.withValidation = withValidation;