/// <summary> /// Validates the message against the schema from the given path. /// </summary> /// <param name="message"><see cref="BrokeredMessage"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="schemaPathPropertyKey">Property key for the schema path.</param> /// <returns>Returns the <see cref="BrokeredMessage"/> instance, if validated; otherwise throws an exception.</returns> public static async Task <BrokeredMessage> ValidateAsync(this BrokeredMessage message, ISchemaValidator validator, string schemaPathPropertyKey = "schemaPath") { message.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); schemaPathPropertyKey.ThrowIfNullOrWhiteSpace(); var cloned = message.Clone(); var payload = default(string); using (var stream = cloned.GetBody <Stream>()) using (var reader = new StreamReader(stream)) { payload = await reader.ReadToEndAsync().ConfigureAwait(false); } if (payload.IsNullOrWhiteSpace()) { throw new MessageBodyZeroLengthException().WithServiceBusMessage(cloned); } var path = cloned.Properties[schemaPathPropertyKey] as string; if (path.IsNullOrWhiteSpace()) { throw new SchemaPathNotExistException().WithServiceBusMessage(cloned); } var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(message); }
private static async Task <bool> ValidateAsync(HttpRequestMessage request, ISchemaValidator validator, string path) { var payload = await request.Content.ReadAsStringAsync().ConfigureAwait(false); var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(validated); }
private async Task <IEnumerable <Error> > GetValidationErrorsAsync(Route route, ExpandoObject payload, string schema) { if (string.IsNullOrWhiteSpace(schema)) { return(Enumerable.Empty <Error>()); } return(await _schemaValidator.ValidateAsync(JsonConvert.SerializeObject(payload), schema)); }
public async Task <IEnumerable <Error> > GetValidationErrorsAsync(PayloadSchema payloadSchema) { if (string.IsNullOrWhiteSpace(payloadSchema.Schema)) { return(Enumerable.Empty <Error>()); } return(await _schemaValidator.ValidateAsync(JsonConvert.SerializeObject(payloadSchema.Payload), payloadSchema.Schema)); }
/// <summary> /// Validates the payload against the schema from the sink. /// </summary> /// <param name="payload">JSON payload.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path in the sink.</param> /// <returns>Returns the payload, if the payload is valid; otherwise throws <see cref="SchemaValidationException"/>.</returns> public static async Task <string> ValidateAsStringAsync(this string payload, ISchemaValidator validator, string path) { payload.ThrowIfNullOrWhiteSpace(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(payload); }
private static async Task <bool> ValidateAsync(string payload, ISchemaValidator validator, string path) { var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(validated); }