mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-05 00:06:20 +02:00
125 lines
4.6 KiB
JavaScript
125 lines
4.6 KiB
JavaScript
"use strict";
|
|
/*
|
|
* @adonisjs/validator
|
|
*
|
|
* (c) Harminder Virk <virk@adonisjs.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
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 (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getArrayType = exports.getObjectType = exports.getLiteralType = exports.compileRule = void 0;
|
|
const Rules_1 = require("./Rules");
|
|
const validations = __importStar(require("./Validations"));
|
|
/**
|
|
* Compiles the `Rule` object and returns `ParsedRule` object.
|
|
*/
|
|
function compileRule(type, subtype, rule, tree) {
|
|
const ruleValidation = validations[rule.name];
|
|
if (!ruleValidation) {
|
|
throw new Error(`"${rule.name}" rule is not registered. Use "validator.rule" to add the rule.`);
|
|
}
|
|
if (typeof ruleValidation.compile !== 'function') {
|
|
throw new Error(`"${rule.name}" rule must implement the compile function`);
|
|
}
|
|
if (typeof ruleValidation.validate !== 'function') {
|
|
throw new Error(`"${rule.name}" rule must implement the validate function`);
|
|
}
|
|
const options = ruleValidation.compile(type, subtype, rule.options, tree);
|
|
tree[rule.name] = options.compiledOptions;
|
|
return options;
|
|
}
|
|
exports.compileRule = compileRule;
|
|
/**
|
|
* Dry function to define a literal type
|
|
*/
|
|
function getLiteralType(subtype, optional, nullable, ruleOptions, rules) {
|
|
const subTypeRule = rules.find((rule) => rule.name === subtype);
|
|
const optionsTree = {};
|
|
return {
|
|
getTree() {
|
|
return {
|
|
type: 'literal',
|
|
nullable,
|
|
optional,
|
|
subtype: subtype,
|
|
rules: []
|
|
.concat(optional ? [] : nullable ? [Rules_1.rules.nullable()] : [Rules_1.rules.required()])
|
|
.concat(subTypeRule ? [] : [Rules_1.rules[subtype](ruleOptions)])
|
|
.concat(rules)
|
|
.map((rule) => compileRule('literal', subtype, rule, optionsTree)),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
exports.getLiteralType = getLiteralType;
|
|
/**
|
|
* Dry function to define an object type
|
|
*/
|
|
function getObjectType(optional, nullable, children, rules) {
|
|
const subTypeRule = rules.find((rule) => rule.name === 'object');
|
|
const optionsTree = {};
|
|
return {
|
|
getTree() {
|
|
return {
|
|
type: 'object',
|
|
nullable,
|
|
optional,
|
|
rules: []
|
|
.concat(optional ? [] : nullable ? [Rules_1.rules.nullable()] : [Rules_1.rules.required()])
|
|
.concat(subTypeRule ? [] : [{ name: 'object', options: [] }])
|
|
.concat(rules)
|
|
.map((rule) => compileRule('object', 'object', rule, optionsTree)),
|
|
...(children ? { children } : {}),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
exports.getObjectType = getObjectType;
|
|
/**
|
|
* Dry function to define an array type
|
|
*/
|
|
function getArrayType(optional, nullable, each, rules) {
|
|
const subTypeRule = rules.find((rule) => rule.name === 'array');
|
|
const optionsTree = {};
|
|
return {
|
|
getTree() {
|
|
return {
|
|
type: 'array',
|
|
nullable,
|
|
optional,
|
|
rules: []
|
|
.concat(optional ? [] : nullable ? [Rules_1.rules.nullable()] : [Rules_1.rules.required()])
|
|
.concat(subTypeRule ? [] : [{ name: 'array', options: [] }])
|
|
.concat(rules)
|
|
.map((rule) => compileRule('array', 'array', rule, optionsTree)),
|
|
...(each ? { each } : {}),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
exports.getArrayType = getArrayType;
|