# invalid_ingredient

> parseIngredientLine received blank text or a value that isn't text, the only input it fails on.

Source: https://recipe-book.siraj.page/errors/invalid_ingredient/

[`parseIngredientLine`](https://recipe-book.siraj.page/reference/core/parsing.md#parseingredientline) received an ingredient line it can't use at all: blank text, or a value that isn't text. It returns a [`RecipeValidationError`](https://recipe-book.siraj.page/reference/core/errors.md#recipevalidationerror) with the code `invalid_ingredient` in `result.error`, and the error's `issues` hold one issue that says what's wrong.

```txt
RecipeValidationError: "" has 1 problem:
  ✖ (root)  Add the ingredient text, e.g. "2 cups flour".
See https://recipe-book.siraj.page/errors/invalid_ingredient/
```

Every other line succeeds, including the [lines the parser can't read](https://recipe-book.siraj.page/errors/invalid_ingredient.md#lines-that-cant-be-read).

| Received                                                | Issue code                                                                      | Message                                                                |
| ------------------------------------------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `''`, or only whitespace and zero-width spaces (U+200B) | [`missing_field`](https://recipe-book.siraj.page/errors.md#issue-missing_field) | `Add the ingredient text, e.g. "2 cups flour".`                        |
| A number, `42`                                          | [`invalid_type`](https://recipe-book.siraj.page/errors.md#issue-invalid_type)   | `Expected ingredient text such as "2 cups flour", received 42.`        |
| An object                                               | `invalid_type`                                                                  | `Expected ingredient text such as "2 cups flour", received an object.` |

Other values that aren't text get the same message with their own ending, such as `received null.`, `received undefined.`, `received true.`, or `received an array.` A value that isn't text has no text to quote, so the error's first line names it `Ingredient`:

```txt
RecipeValidationError: Ingredient has 1 problem:
  ✖ (root)  Expected ingredient text such as "2 cups flour", received 42.
See https://recipe-book.siraj.page/errors/invalid_ingredient/
```

The text is the whole input, so the issue's path is empty, which the message writes as `(root)`. In recipe input parsed with `{ parseIngredientLine }`, a blank line is a `missing_field` issue in an [`invalid_recipe`](https://recipe-book.siraj.page/errors/invalid_recipe.md) error, at the line's path.

## Lines that can't be read

These lines succeed, keep the whole line as the name, and return one warning in `result.warnings`. Its code is one of the [issue codes](https://recipe-book.siraj.page/errors.md#issue-codes) that are errors elsewhere, at severity `'warning'`:

| Line                                            | Issue code                                                                            | Message                                                                                                                                           |
| ----------------------------------------------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `'2 or 3 eggs'`                                 | [`invalid_quantity`](https://recipe-book.siraj.page/errors.md#issue-invalid_quantity) | `Couldn't read the quantity in "2 or 3 eggs". Write one amount or range before the unit, e.g. "2-3 eggs" or "2 ½ cups". …`                        |
| `'1 cup + 1 egg'`                               | `invalid_quantity`                                                                    | `Expected a measure after "+" in the same kind of unit, e.g. "1 cup plus 2 tbsp". …`                                                              |
| `'100 g/flour'`                                 | `invalid_quantity`                                                                    | `Expected a measure after "/", e.g. "100 g/3½ oz". …`                                                                                             |
| `'1,500 kg potatoes'`                           | `invalid_quantity`                                                                    | `Couldn't read "1,500 kg potatoes". "1,500" could be "1500" or "1.5": write one of those, or pass { decimal: "," } to read commas as decimals. …` |
| `'1,500.5 g flour'`                             | `invalid_quantity`                                                                    | `Couldn't read "1,500.5 g flour". Leave out thousands separators, e.g. "1500.5". …`                                                               |
| `'0 eggs'`                                      | [`invalid_amount`](https://recipe-book.siraj.page/errors.md#issue-invalid_amount)     | `Expected a positive amount, received "0 eggs". …`                                                                                                |
| `'9'.repeat(400) + ' g flour'`                  | `invalid_amount`                                                                      | `"99999999999999999999999999999999999999999999999999999999999…" has an amount too large to read. Check it for extra digits. …`                    |
| `'2 x ' + '9'.repeat(308) + ' g cans tomatoes'` | `invalid_amount`                                                                      | `The total amount is out of range. Write it as one amount, e.g. "800 g". …`                                                                       |
| `'3-2 cups milk'`                               | [`invalid_range`](https://recipe-book.siraj.page/errors.md#issue-invalid_range)       | `Range minimum (3) must not be greater than the maximum (2). …`                                                                                   |
| `'1 – ½ cups flour'`                            | `invalid_range`                                                                       | `Range minimum (1) must not be greater than the maximum (½). To write a mixed number, use "1 ½". …`                                               |
| `'2 cups'`                                      | `missing_field`                                                                       | `Add a name after the quantity, e.g. "2 cups flour". …`                                                                                           |

Each message ends with `The whole line is kept as the name.`

## Misspelled units

When the word after the amount is a likely misspelling of a unit, as in `'2 cupz flour'`, the line parses as it would without a warning: the word stays in the name, and the quantity has no unit. It also returns one warning, whose `suggestion` is the unit that [`parseQuantity`](https://recipe-book.siraj.page/reference/core/parsing.md#parsequantity) suggests for the same word. When units of more than one dimension are a letter off, as `lbs` and `tbsp` are from `lbsp`, that's still one of them, so check a suggestion before you apply it, since it can turn a volume into a weight:

| Line              | Issue code                                                                    | Message                                                                       |
| ----------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| `'2 cupz flour'`  | [`unknown_unit`](https://recipe-book.siraj.page/errors.md#issue-unknown_unit) | `"cupz" isn't a unit, so it's read as part of the name. Did you mean "cups"?` |
| `'2 lbsp butter'` | `unknown_unit`                                                                | `"lbsp" isn't a unit, so it's read as part of the name. Did you mean "lbs"?`  |

Most real words that are a letter off a unit don't warn, such as `'pots'` (`pts`), `'pink'` (`pint`), `'pins'` (`pints`), `'sliced'` (`slice`), and `'spring'` (`sprig`). One with a letter doubled or two letters swapped after its first two still does, as `'litter'` (`liter`) and `'cusp'` (`cups`) do.

## Fix the ingredient line

Pass text, and skip blank lines before parsing. A line of only whitespace and zero-width spaces (U+200B), which text copied from a web page can hold, is blank to `parseIngredientLine`, but `trim()` keeps the U+200B, so remove it before you test the line:

```ts {7}
import { parseIngredientLine } from '@recipe-book/core'

parseIngredientLine('').success // false
parseIngredientLine('2 cups flour') // { success: true, data: { name: 'flour', quantity: { amount: 2, unit: 'cup' } }, warnings: [] }

const pasted = '2 cups flour\n\n\u200b\n1 tsp salt'
const lines = pasted.split('\n').filter((line) => line.replace(/\u200b/g, '').trim() !== '')
lines.map((line) => parseIngredientLine(line).success) // [true, true]
```

When a recipe writes decimals with a comma, pass [`decimal: ','`](https://recipe-book.siraj.page/reference/core/parsing.md#parsequantityoptions). Without it, `parseIngredientLine` reads a comma between digits that can only be a decimal, as in `'1,5 kg potatoes'`, but keeps a line such as `'1,500 kg potatoes'` whole as the name, with an `invalid_quantity` warning.
