New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@envelop/extended-validation

Package Overview
Dependencies
Maintainers
1
Versions
1363
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@envelop/extended-validation - npm Package Compare versions

Comparing version 4.0.0 to 4.1.0-alpha-20240806125641-9b9885ba

56

cjs/plugin.js

@@ -5,2 +5,3 @@ "use strict";

const graphql_1 = require("graphql");
const core_1 = require("@envelop/core");
const symbolExtendedValidationRules = Symbol('extendedValidationContext');

@@ -31,8 +32,8 @@ const useExtendedValidation = (options) => {

},
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed),
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed),
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false),
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false),
};
};
exports.useExtendedValidation = useExtendedValidation;
function buildHandler(name, getTypeInfo, onValidationFailed) {
function buildHandler(name, getTypeInfo, onValidationFailed, rejectOnErrors = true) {
return function handler({ args, setResultAndStopExecution, }) {

@@ -59,12 +60,45 @@ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind

const visitor = (0, graphql_1.visitInParallel)(validationRulesContext.rules.map(rule => rule(validationContext, args)));
(0, graphql_1.visit)(args.document, (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor));
args.document = (0, graphql_1.visit)(args.document, (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor));
if (errors.length > 0) {
let result = {
data: null,
errors,
};
if (onValidationFailed) {
onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
if (rejectOnErrors) {
let result = {
data: null,
errors,
};
if (onValidationFailed) {
onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
}
setResultAndStopExecution(result);
}
setResultAndStopExecution(result);
else {
// eslint-disable-next-line no-inner-declarations
function onResult({ result, setResult, }) {
if ((0, core_1.isAsyncIterable)(result)) {
// rejectOnErrors is false doesn't work with async iterables
setResult({
data: null,
errors,
});
return;
}
const newResult = {
...result,
errors: [...(result.errors || []), ...errors],
};
errors.forEach(e => {
if (e.path?.length) {
let currentData = (newResult.data ||= {});
for (const pathItem of e.path.slice(0, -1)) {
currentData = currentData[pathItem] ||= {};
}
currentData[e.path[e.path.length - 1]] = null;
}
});
setResult(newResult);
}
return {
onSubscribeResult: onResult,
onExecuteDone: onResult,
};
}
}

@@ -71,0 +105,0 @@ }

import { TypeInfo, ValidationContext, visit, visitInParallel, visitWithTypeInfo, } from 'graphql';
import { isAsyncIterable, } from '@envelop/core';
const symbolExtendedValidationRules = Symbol('extendedValidationContext');

@@ -27,7 +28,7 @@ export const useExtendedValidation = (options) => {

},
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed),
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed),
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false),
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false),
};
};
function buildHandler(name, getTypeInfo, onValidationFailed) {
function buildHandler(name, getTypeInfo, onValidationFailed, rejectOnErrors = true) {
return function handler({ args, setResultAndStopExecution, }) {

@@ -54,12 +55,45 @@ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind

const visitor = visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args)));
visit(args.document, visitWithTypeInfo(typeInfo, visitor));
args.document = visit(args.document, visitWithTypeInfo(typeInfo, visitor));
if (errors.length > 0) {
let result = {
data: null,
errors,
};
if (onValidationFailed) {
onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
if (rejectOnErrors) {
let result = {
data: null,
errors,
};
if (onValidationFailed) {
onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
}
setResultAndStopExecution(result);
}
setResultAndStopExecution(result);
else {
// eslint-disable-next-line no-inner-declarations
function onResult({ result, setResult, }) {
if (isAsyncIterable(result)) {
// rejectOnErrors is false doesn't work with async iterables
setResult({
data: null,
errors,
});
return;
}
const newResult = {
...result,
errors: [...(result.errors || []), ...errors],
};
errors.forEach(e => {
if (e.path?.length) {
let currentData = (newResult.data ||= {});
for (const pathItem of e.path.slice(0, -1)) {
currentData = currentData[pathItem] ||= {};
}
currentData[e.path[e.path.length - 1]] = null;
}
});
setResult(newResult);
}
return {
onSubscribeResult: onResult,
onExecuteDone: onResult,
};
}
}

@@ -66,0 +100,0 @@ }

2

package.json
{
"name": "@envelop/extended-validation",
"version": "4.0.0",
"version": "4.1.0-alpha-20240806125641-9b9885ba",
"sideEffects": false,

@@ -5,0 +5,0 @@ "peerDependencies": {

@@ -20,2 +20,8 @@ import { ExecutionArgs, ExecutionResult } from 'graphql';

onValidationFailed?: OnValidationFailedCallback;
/**
* Reject the execution if the validation fails.
*
* @default true
*/
rejectOnErrors?: boolean;
}) => Plugin<PluginContext & {

@@ -22,0 +28,0 @@ [symbolExtendedValidationRules]?: ExtendedValidationContext | undefined;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc