本文整理汇总了C#中JToken.IsValid方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.IsValid方法的具体用法?C# JToken.IsValid怎么用?C# JToken.IsValid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.IsValid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
/// <summary>
/// Perform validation for token
/// </summary>
public IValidation Validate(JToken item)
{
var assembly = Assembly.GetExecutingAssembly();
const string resourceName = "Foobricator.Schema.json";
JSchema schema;
using (Stream manifestStream = assembly.GetManifestResourceStream(resourceName))
{
schema = JSchema.Load(new JsonTextReader(new StreamReader(manifestStream)));
}
IList<ValidationError> messages;
var result = item.IsValid(schema, out messages);
return new RichValidation(result, messages);
}
示例2: Validate
private static bool Validate(JToken obj, string path, string schemaPath)
{
JSchemaUrlResolver resolver = new JSchemaUrlResolver();
IList<ValidationError> errors;
bool valid = true;
using (StreamReader file = File.OpenText(schemaPath))
using (JsonTextReader reader = new JsonTextReader(file))
{
JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings { Resolver = resolver, BaseUri = new Uri(schemaPath) });
try
{
valid = obj.IsValid(schema, out errors);
}
catch (JSchemaException e)
{
errors = new List<ValidationError>();
if (e.Message.Contains("free-quota limit"))
{
valid = true;
}
else
{
valid = false;
}
}
if (valid)
{
return true;
}
else
{
foreach (var error in errors)
{
Console.Error.WriteLine(error.ToString());
}
throw new InvalidDataException("Could not validate " + path);
}
}
}