本文整理汇总了C#中ValidationContext.ReportError方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.ReportError方法的具体用法?C# ValidationContext.ReportError怎么用?C# ValidationContext.ReportError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ValidationContext
的用法示例。
在下文中一共展示了ValidationContext.ReportError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<InlineFragment>(node =>
{
var type = context.TypeInfo.GetLastType();
if (node.Type != null && type != null && !type.IsCompositeType())
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.4.1.3",
InlineFragmentOnNonCompositeErrorMessage(context.Print(node.Type)),
node.Type));
}
});
_.Match<FragmentDefinition>(node =>
{
var type = context.TypeInfo.GetLastType();
if (type != null && !type.IsCompositeType())
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.4.1.3",
FragmentOnNonCompositeErrorMessage(node.Name, context.Print(node.Type)),
node.Type));
}
});
});
}
示例2: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<Directive>(node =>
{
var directiveDef = context.Schema.Directives.SingleOrDefault(x => x.Name == node.Name);
if (directiveDef == null)
{
context.ReportError(new ValidationError(context.OriginalQuery, "5.6.1", UnknownDirectiveMessage(node.Name), node));
return;
}
var canidateLocation = getDirectiveLocationForAstPath(context.TypeInfo.GetAncestors(), context);
if (!directiveDef.Locations.Any(x => x == canidateLocation))
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.6.1",
MisplacedDirectiveMessage(node.Name, canidateLocation.ToString()),
node));
}
});
});
}
示例3: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<Field>(leave: node =>
{
var fieldDef = context.TypeInfo.GetFieldDef();
if (fieldDef == null)
{
return;
}
fieldDef.Arguments?.Apply(arg =>
{
var argAst = node.Arguments?.ValueFor(arg.Name);
var type = context.Schema.FindType(arg.Type);
if (argAst == null && type is NonNullGraphType)
{
context.ReportError(
new ValidationError(
context.OriginalQuery,
"5.3.3.2",
MissingFieldArgMessage(node.Name, arg.Name, context.Print(type)),
node));
}
});
});
_.Match<Directive>(leave: node =>
{
var directive = context.TypeInfo.GetDirective();
if (directive == null)
{
return;
}
directive.Arguments?.Apply(arg =>
{
var argAst = node.Arguments?.ValueFor(arg.Name);
var type = context.Schema.FindType(arg.Type);
if (argAst == null && type is NonNullGraphType)
{
context.ReportError(
new ValidationError(
context.OriginalQuery,
"5.3.3.2",
MissingDirectiveArgMessage(node.Name, arg.Name, context.Print(type)),
node));
}
});
});
});
}
示例4: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<Argument>(node =>
{
var ancestors = context.TypeInfo.GetAncestors();
var argumentOf = ancestors[ancestors.Length - 2];
if (argumentOf is Field)
{
var fieldDef = context.TypeInfo.GetFieldDef();
if (fieldDef != null)
{
var fieldArgDef = fieldDef.Arguments?.Find(node.Name);
if (fieldArgDef == null)
{
var parentType = context.TypeInfo.GetParentType();
Invariant.Check(parentType != null, "Parent type must not be null.");
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.3.1",
UnknownArgMessage(
node.Name,
fieldDef.Name,
context.Print(parentType),
StringUtils.SuggestionList(node.Name, fieldDef.Arguments?.Select(q => q.Name))),
node));
}
}
} else if (argumentOf is Directive)
{
var directive = context.TypeInfo.GetDirective();
if (directive != null)
{
var directiveArgDef = directive.Arguments?.Find(node.Name);
if (directiveArgDef == null)
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.3.1",
UnknownDirectiveArgMessage(
node.Name,
directive.Name,
StringUtils.SuggestionList(node.Name, directive.Arguments?.Select(q => q.Name))),
node));
}
}
}
});
});
}
示例5: Validate
public INodeVisitor Validate(ValidationContext context)
{
var operationDefs = new List<Operation>();
var fragmentDefs = new List<FragmentDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<Operation>(node => operationDefs.Add(node));
_.Match<FragmentDefinition>(node => fragmentDefs.Add(node));
_.Match<Document>(
leave: document =>
{
var fragmentNameUsed = new List<string>();
operationDefs.Apply(operation =>
{
context.GetRecursivelyReferencedFragments(operation).Apply(fragment =>
{
fragmentNameUsed.Add(fragment.Name);
});
});
fragmentDefs.Apply(fragmentDef =>
{
var fragName = fragmentDef.Name;
if (!fragmentNameUsed.Contains(fragName))
{
var error = new ValidationError(context.OriginalQuery, "5.4.1.4", UnusedFragMessage(fragName), fragmentDef);
context.ReportError(error);
}
});
});
});
}
示例6: Validate
public INodeVisitor Validate(ValidationContext context)
{
var frequency = new Dictionary<string, string>();
return new EnterLeaveListener(_ =>
{
_.Match<Operation>(
enter: op =>
{
if (context.Document.Operations.Count < 2)
{
return;
}
if (string.IsNullOrWhiteSpace(op.Name))
{
return;
}
if (frequency.ContainsKey(op.Name))
{
var error = new ValidationError(
context.OriginalQuery,
"5.1.1.1",
DuplicateOperationNameMessage(op.Name),
op);
context.ReportError(error);
}
else
{
frequency[op.Name] = op.Name;
}
});
});
}
示例7: Validate
public INodeVisitor Validate(ValidationContext context)
{
var variableNameDefined = new Dictionary<string, bool>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(varDef => variableNameDefined[varDef.Name] = true);
_.Match<Operation>(
enter: op => variableNameDefined = new Dictionary<string, bool>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op);
usages.Apply(usage =>
{
var varName = usage.Node.Name;
bool found;
if (!variableNameDefined.TryGetValue(varName, out found))
{
var error = new ValidationError(
context.OriginalQuery,
"5.7.4",
UndefinedVarMessage(varName, op.Name),
usage.Node,
op);
context.ReportError(error);
}
});
});
});
}
示例8: Validate
public INodeVisitor Validate(ValidationContext context)
{
var knownFragments = new Dictionary<string, FragmentDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<FragmentDefinition>(fragmentDefinition =>
{
var fragmentName = fragmentDefinition.Name;
if (knownFragments.ContainsKey(fragmentName))
{
var error = new ValidationError(
context.OriginalQuery,
"5.4.1.1",
DuplicateFragmentNameMessage(fragmentName),
knownFragments[fragmentName],
fragmentDefinition);
context.ReportError(error);
}
else
{
knownFragments[fragmentName] = fragmentDefinition;
}
});
});
}
示例9: Validate
public INodeVisitor Validate(ValidationContext context)
{
var knownArgs = new Dictionary<string, Argument>();
return new EnterLeaveListener(_ =>
{
_.Match<Field>(field => knownArgs = new Dictionary<string, Argument>());
_.Match<Directive>(field => knownArgs = new Dictionary<string, Argument>());
_.Match<Argument>(argument =>
{
var argName = argument.Name;
if (knownArgs.ContainsKey(argName))
{
var error = new ValidationError(context.OriginalQuery,
"5.3.2",
DuplicateArgMessage(argName),
knownArgs[argName],
argument);
context.ReportError(error);
}
else
{
knownArgs[argName] = argument;
}
});
});
}
示例10: Validate
public INodeVisitor Validate(ValidationContext context)
{
var variableDefs = new List<VariableDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(def => variableDefs.Add(def));
_.Match<Operation>(
enter: op => variableDefs = new List<VariableDefinition>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op).Select(usage => usage.Node.Name);
variableDefs.Apply(variableDef =>
{
var variableName = variableDef.Name;
if (!usages.Contains(variableName))
{
var error = new ValidationError(context.OriginalQuery, "5.7.5", UnusedVariableMessage(variableName, op.Name), variableDef);
context.ReportError(error);
}
});
});
});
}
示例11: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(varDefAst =>
{
var name = varDefAst.Name;
var defaultValue = varDefAst.DefaultValue;
var inputType = context.TypeInfo.GetInputType();
if (inputType is NonNullGraphType && defaultValue != null)
{
var nonNullType = (NonNullGraphType) inputType;
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.7.2",
BadValueForNonNullArgMessage(
name,
context.Print(inputType),
context.Print(nonNullType.ResolvedType)),
defaultValue));
}
if (inputType != null && defaultValue != null)
{
var errors = inputType.IsValidLiteralValue(defaultValue, context.Schema).ToList();
if (errors.Any())
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.7.2",
BadValueForDefaultArgMessage(
name,
context.Print(inputType),
context.Print(defaultValue),
errors),
defaultValue));
}
}
});
});
}
示例12: Field
private void Field(GraphType type, Field field, ValidationContext context)
{
if (type == null)
{
return;
}
if (type.IsLeafType(context.Schema))
{
if (field.SelectionSet != null && field.SelectionSet.Selections.Any())
{
var error = new ValidationError(context.OriginalQuery, "5.2.3", NoSubselectionAllowedMessage(field.Name, context.Print(type)), field.SelectionSet);
context.ReportError(error);
}
}
else if(field.SelectionSet == null || !field.SelectionSet.Selections.Any())
{
var error = new ValidationError(context.OriginalQuery, "5.2.3", RequiredSubselectionMessage(field.Name, context.Print(type)), field);
context.ReportError(error);
}
}
示例13: Validate
public INodeVisitor Validate(ValidationContext context)
{
var varDefMap = new Dictionary<string, VariableDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(
varDefAst => varDefMap[varDefAst.Name] = varDefAst
);
_.Match<Operation>(
enter: op => varDefMap = new Dictionary<string, VariableDefinition>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op);
usages.Apply(usage =>
{
var varName = usage.Node.Name;
VariableDefinition varDef;
if (!varDefMap.TryGetValue(varName, out varDef))
{
return;
}
if (varDef != null && usage.Type != null)
{
var varType = varDef.Type.GraphTypeFromType(context.Schema);
if (varType != null &&
!effectiveType(varType, varDef).IsSubtypeOf(usage.Type, context.Schema))
{
var error = new ValidationError(
context.OriginalQuery,
"5.7.6",
BadVarPosMessage(varName, context.Print(varType), context.Print(usage.Type)));
var source = new Source(context.OriginalQuery);
var varDefPos = new Location(source, varDef.SourceLocation.Start);
var usagePos = new Location(source, usage.Node.SourceLocation.Start);
error.AddLocation(varDefPos.Line, varDefPos.Column);
error.AddLocation(usagePos.Line, usagePos.Column);
context.ReportError(error);
}
}
});
}
);
});
}
示例14: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(varDef =>
{
var type = varDef.Type.GraphTypeFromType(context.Schema);
if (!type.IsInputType())
{
context.ReportError(new ValidationError(context.OriginalQuery, "5.7.3", UndefinedVarMessage(varDef.Name, type != null ? context.Print(type) : varDef.Type.Name()), varDef));
}
});
});
}
示例15: Validate
public INodeVisitor Validate(ValidationContext context)
{
return new EnterLeaveListener(_ =>
{
_.Match<InlineFragment>(node =>
{
var fragType = context.TypeInfo.GetLastType();
var parentType = context.TypeInfo.GetParentType().GetNamedType(context.Schema);
if (fragType != null && parentType != null && !context.Schema.DoTypesOverlap(fragType, parentType))
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.4.2.3",
TypeIncompatibleAnonSpreadMessage(context.Print(parentType), context.Print(fragType)),
node));
}
});
_.Match<FragmentSpread>(node =>
{
var fragName = node.Name;
var fragType = getFragmentType(context, fragName);
var parentType = context.TypeInfo.GetParentType().GetNamedType(context.Schema);
if (fragType != null && parentType != null && !context.Schema.DoTypesOverlap(fragType, parentType))
{
context.ReportError(new ValidationError(
context.OriginalQuery,
"5.4.2.3",
TypeIncompatibleSpreadMessage(fragName, context.Print(parentType), context.Print(fragType)),
node));
}
});
});
}