Warning: session_start(): Session cannot be started after headers have already been sent in /home/tvrreohg/public_html/manga.php on line 13
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
const {
stringHints,
numberHints
} = require('./util/hints');
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
/** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
/** @typedef {import("./validate").Schema} Schema */
/** @typedef {import("./validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
/** @typedef {import("./validate").PostFormatter} PostFormatter */
/** @typedef {import("./validate").SchemaUtilErrorObject} SchemaUtilErrorObject */
/** @enum {number} */
const SPECIFICITY = {
type: 1,
not: 1,
oneOf: 1,
anyOf: 1,
if: 1,
enum: 1,
const: 1,
instanceof: 1,
required: 2,
pattern: 2,
patternRequired: 2,
format: 2,
formatMinimum: 2,
formatMaximum: 2,
minimum: 2,
exclusiveMinimum: 2,
maximum: 2,
exclusiveMaximum: 2,
multipleOf: 2,
uniqueItems: 2,
contains: 2,
minLength: 2,
maxLength: 2,
minItems: 2,
maxItems: 2,
minProperties: 2,
maxProperties: 2,
dependencies: 2,
propertyNames: 2,
additionalItems: 2,
additionalProperties: 2,
absolutePath: 2
};
/**
*
* @param {Array} array
* @param {(item: SchemaUtilErrorObject) => number} fn
* @returns {Array}
*/
function filterMax(array, fn) {
const evaluatedMax = array.reduce((max, item) => Math.max(max, fn(item)), 0);
return array.filter(item => fn(item) === evaluatedMax);
}
/**
*
* @param {Array} children
* @returns {Array}
*/
function filterChildren(children) {
let newChildren = children;
newChildren = filterMax(newChildren,
/**
*
* @param {SchemaUtilErrorObject} error
* @returns {number}
*/
error => error.dataPath ? error.dataPath.length : 0);
newChildren = filterMax(newChildren,
/**
* @param {SchemaUtilErrorObject} error
* @returns {number}
*/
error => SPECIFICITY[
/** @type {keyof typeof SPECIFICITY} */
error.keyword] || 2);
return newChildren;
}
/**
* Find all children errors
* @param {Array} children
* @param {Array} schemaPaths
* @return {number} returns index of first child
*/
function findAllChildren(children, schemaPaths) {
let i = children.length - 1;
const predicate =
/**
* @param {string} schemaPath
* @returns {boolean}
*/
schemaPath => children[i].schemaPath.indexOf(schemaPath) !== 0;
while (i > -1 && !schemaPaths.every(predicate)) {
if (children[i].keyword === 'anyOf' || children[i].keyword === 'oneOf') {
const refs = extractRefs(children[i]);
const childrenStart = findAllChildren(children.slice(0, i), refs.concat(children[i].schemaPath));
i = childrenStart - 1;
} else {
i -= 1;
}
}
return i + 1;
}
/**
* Extracts all refs from schema
* @param {SchemaUtilErrorObject} error
* @return {Array}
*/
function extractRefs(error) {
const {
schema
} = error;
if (!Array.isArray(schema)) {
return [];
}
return schema.map(({
$ref
}) => $ref).filter(s => s);
}
/**
* Groups children by their first level parent (assuming that error is root)
* @param {Array} children
* @return {Array}
*/
function groupChildrenByFirstChild(children) {
const result = [];
let i = children.length - 1;
while (i > 0) {
const child = children[i];
if (child.keyword === 'anyOf' || child.keyword === 'oneOf') {
const refs = extractRefs(child);
const childrenStart = findAllChildren(children.slice(0, i), refs.concat(child.schemaPath));
if (childrenStart !== i) {
result.push(Object.assign({}, child, {
children: children.slice(childrenStart, i)
}));
i = childrenStart;
} else {
result.push(child);
}
} else {
result.push(child);
}
i -= 1;
}
if (i === 0) {
result.push(children[i]);
}
return result.reverse();
}
/**
* @param {string} str
* @param {string} prefix
* @returns {string}
*/
function indent(str, prefix) {
return str.replace(/\n(?!$)/g, `\n${prefix}`);
}
/**
* @param {Schema} schema
* @returns {schema is (Schema & {not: Schema})}
*/
function hasNotInSchema(schema) {
return !!schema.not;
}
/**
* @param {Schema} schema
* @return {Schema}
*/
function findFirstTypedSchema(schema) {
if (hasNotInSchema(schema)) {
return findFirstTypedSchema(schema.not);
}
return schema;
}
/**
* @param {Schema} schema
* @return {boolean}
*/
function canApplyNot(schema) {
const typedSchema = findFirstTypedSchema(schema);
return likeNumber(typedSchema) || likeInteger(typedSchema) || likeString(typedSchema) || likeNull(typedSchema) || likeBoolean(typedSchema);
}
/**
* @param {any} maybeObj
* @returns {boolean}
*/
function isObject(maybeObj) {
return typeof maybeObj === 'object' && maybeObj !== null;
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeNumber(schema) {
return schema.type === 'number' || typeof schema.minimum !== 'undefined' || typeof schema.exclusiveMinimum !== 'undefined' || typeof schema.maximum !== 'undefined' || typeof schema.exclusiveMaximum !== 'undefined' || typeof schema.multipleOf !== 'undefined';
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeInteger(schema) {
return schema.type === 'integer' || typeof schema.minimum !== 'undefined' || typeof schema.exclusiveMinimum !== 'undefined' || typeof schema.maximum !== 'undefined' || typeof schema.exclusiveMaximum !== 'undefined' || typeof schema.multipleOf !== 'undefined';
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeString(schema) {
return schema.type === 'string' || typeof schema.minLength !== 'undefined' || typeof schema.maxLength !== 'undefined' || typeof schema.pattern !== 'undefined' || typeof schema.format !== 'undefined' || typeof schema.formatMinimum !== 'undefined' || typeof schema.formatMaximum !== 'undefined';
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeBoolean(schema) {
return schema.type === 'boolean';
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeArray(schema) {
return schema.type === 'array' || typeof schema.minItems === 'number' || typeof schema.maxItems === 'number' || typeof schema.uniqueItems !== 'undefined' || typeof schema.items !== 'undefined' || typeof schema.additionalItems !== 'undefined' || typeof schema.contains !== 'undefined';
}
/**
* @param {Schema & {patternRequired?: Array}} schema
* @returns {boolean}
*/
function likeObject(schema) {
return schema.type === 'object' || typeof schema.minProperties !== 'undefined' || typeof schema.maxProperties !== 'undefined' || typeof schema.required !== 'undefined' || typeof schema.properties !== 'undefined' || typeof schema.patternProperties !== 'undefined' || typeof schema.additionalProperties !== 'undefined' || typeof schema.dependencies !== 'undefined' || typeof schema.propertyNames !== 'undefined' || typeof schema.patternRequired !== 'undefined';
}
/**
* @param {Schema} schema
* @returns {boolean}
*/
function likeNull(schema) {
return schema.type === 'null';
}
/**
* @param {string} type
* @returns {string}
*/
function getArticle(type) {
if (/^[aeiou]/i.test(type)) {
return 'an';
}
return 'a';
}
/**
* @param {Schema=} schema
* @returns {string}
*/
function getSchemaNonTypes(schema) {
if (!schema) {
return '';
}
if (!schema.type) {
if (likeNumber(schema) || likeInteger(schema)) {
return ' | should be any non-number';
}
if (likeString(schema)) {
return ' | should be any non-string';
}
if (likeArray(schema)) {
return ' | should be any non-array';
}
if (likeObject(schema)) {
return ' | should be any non-object';
}
}
return '';
}
/**
* @param {Array} hints
* @returns {string}
*/
function formatHints(hints) {
return hints.length > 0 ? `(${hints.join(', ')})` : '';
}
/**
* @param {Schema} schema
* @param {boolean} logic
* @returns {string[]}
*/
function getHints(schema, logic) {
if (likeNumber(schema) || likeInteger(schema)) {
return numberHints(schema, logic);
} else if (likeString(schema)) {
return stringHints(schema, logic);
}
return [];
}
class ValidationError extends Error {
/**
* @param {Array} errors
* @param {Schema} schema
* @param {ValidationErrorConfiguration} configuration
*/
constructor(errors, schema, configuration = {}) {
super();
/** @type {string} */
this.name = 'ValidationError';
/** @type {Array} */
this.errors = errors;
/** @type {Schema} */
this.schema = schema;
let headerNameFromSchema;
let baseDataPathFromSchema;
if (schema.title && (!configuration.name || !configuration.baseDataPath)) {
const splittedTitleFromSchema = schema.title.match(/^(.+) (.+)$/);
if (splittedTitleFromSchema) {
if (!configuration.name) {
[, headerNameFromSchema] = splittedTitleFromSchema;
}
if (!configuration.baseDataPath) {
[,, baseDataPathFromSchema] = splittedTitleFromSchema;
}
}
}
/** @type {string} */
this.headerName = configuration.name || headerNameFromSchema || 'Object';
/** @type {string} */
this.baseDataPath = configuration.baseDataPath || baseDataPathFromSchema || 'configuration';
/** @type {PostFormatter | null} */
this.postFormatter = configuration.postFormatter || null;
const header = `Invalid ${this.baseDataPath} object. ${this.headerName} has been initialized using ${getArticle(this.baseDataPath)} ${this.baseDataPath} object that does not match the API schema.\n`;
/** @type {string} */
this.message = `${header}${this.formatValidationErrors(errors)}`;
Error.captureStackTrace(this, this.constructor);
}
/**
* @param {string} path
* @returns {Schema}
*/
getSchemaPart(path) {
const newPath = path.split('/');
let schemaPart = this.schema;
for (let i = 1; i < newPath.length; i++) {
const inner = schemaPart[
/** @type {keyof Schema} */
newPath[i]];
if (!inner) {
break;
}
schemaPart = inner;
}
return schemaPart;
}
/**
* @param {Schema} schema
* @param {boolean} logic
* @param {Array