ValidateBytes is like Validate but accepts []byte to avoid a string copy.
(input []byte)
| 37 | |
| 38 | // ValidateBytes is like Validate but accepts []byte to avoid a string copy. |
| 39 | func ValidateBytes(input []byte) error { |
| 40 | // Fast path: empty/whitespace-only input is valid |
| 41 | if len(trimBytes(input)) == 0 { |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | tkz := tokenizer.GetTokenizer() |
| 46 | defer tokenizer.PutTokenizer(tkz) |
| 47 | |
| 48 | tokens, err := tkz.Tokenize(input) |
| 49 | if err != nil { |
| 50 | return fmt.Errorf("tokenization error: %w", err) |
| 51 | } |
| 52 | |
| 53 | if len(tokens) == 0 { |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | p := GetParser() |
| 58 | defer PutParser(p) |
| 59 | |
| 60 | astResult, parseErr := p.ParseFromModelTokens(tokens) |
| 61 | if parseErr != nil { |
| 62 | return parseErr |
| 63 | } |
| 64 | ast.ReleaseAST(astResult) |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // trimBytes returns input with leading/trailing whitespace removed. |
| 69 | func trimBytes(b []byte) []byte { |