public IReadOnlyCollection <ValidationResult> Validate(Token token, ITokenCollection tokenCollection) { var value = tokenCollection.Peek(); if (value is null) { var error = new ParserError($"{token.Name} value is supposed to be string or array.", token.Line, token.Column); return(new[] { ValidationResult.Error(error) }); } if (value.Name == TokenName.StartArray) { var result = _stringArrayWithUniqueValuesValidator.Validate(token, tokenCollection, _arrayFilterers); return(new[] { result }); } else if (value.Name == TokenName.String) { value = tokenCollection.TakeToken(); if (!_typeElementSpecification.IsSatisfied(value)) { var error = new ParserError($"{token.Name} had invalid value.", token.Line, token.Column); return(new[] { ValidationResult.Error(error) }); } } else { var error = new ParserError("Inalid type value.", token.Line, token.Column); return(new[] { ValidationResult.Error(error) }); } return(new[] { ValidationResult.Success() }); }
public IReadOnlyCollection <ValidationResult> Validate(Token token, ITokenCollection tokenCollection) { var errors = new List <ValidationResult>(); var startObjectToken = tokenCollection.TakeToken(); if (startObjectToken?.Name != TokenName.StartObject) { return new[] { Error("Object is supposed to start with '{'.", startObjectToken) } } ; var fieldToken = tokenCollection.TakeToken(); while (fieldToken.Name != TokenName.EndObject) { var colonToken = tokenCollection.TakeToken(); if (fieldToken is null) { errors.Add(Error($"{token.Name} does not have closing curly bracket.", token)); return(errors); } if (colonToken is null || colonToken.Name != TokenName.Colon) { return(new[] { Error("Object field is supposed have colon after identifier", colonToken) }); } if (fieldToken.Name != TokenName.String && !_keywords.Contains(fieldToken.Name)) { return(new[] { Error("Inorrect token. It is supposed to be string", token) }); } var valueToken = tokenCollection.Peek(); if (_keywords.Contains(fieldToken.Name) && _tokenHandlerFactory.GetTokenValidator(fieldToken) != null) { var validator = _tokenHandlerFactory.GetTokenValidator(fieldToken); var result = validator.Validate(valueToken, tokenCollection); errors.AddRange(result); } else if (valueToken.Name == TokenName.StartArray) { var result = _arrayValidator.Validate(valueToken, tokenCollection, Enumerable.Empty <IArrayFilterer>()); errors.Add(result); } else if (valueToken.Name == TokenName.StartObject) { var result = Validate(valueToken, tokenCollection); errors.AddRange(result); } else { var validator = _tokenHandlerFactory.GetTokenValidator(valueToken); var result = validator.Validate(valueToken, tokenCollection); errors.AddRange(result); } var terminationToken = tokenCollection.Peek(); if (terminationToken is null || (terminationToken.Name != TokenName.Comma && terminationToken.Name != TokenName.EndObject)) { errors.Add(Error($"Expected comma or end of object.", valueToken)); return(errors); } if (terminationToken.Name == TokenName.Comma) { tokenCollection.TakeToken(); } fieldToken = tokenCollection.TakeToken(); } return(errors); }
public IReadOnlyCollection <ValidationResult> Validate(Token token, ITokenCollection tokenCollection) { return(new[] { _stringArrayWithUniqueValuesValidator.Validate(token, tokenCollection, _arrayFilterers) }); }
public IReadOnlyCollection <ValidationResult> Validate(Token token, ITokenCollection tokenCollection) { return(new[] { _arrayValidator.Validate(token, tokenCollection, _arrayFilterers) }); }