mofilo_nutrient_verifier 1.0.2 copy "mofilo_nutrient_verifier: ^1.0.2" to clipboard
mofilo_nutrient_verifier: ^1.0.2 copied to clipboard

Nutrition data validator for user-submitted food data. Validates using physics-based limits, energy conservation, and toxicity thresholds. Binary pass/fail with trust scoring.

Mofilo Nutrient Verifier #

Nutrition data validator for user-submitted food data. Checks if data is physically possible and flags values exceeding known toxicity thresholds.

pub package License: MIT


Quick Start #

import 'package:mofilo_nutrient_verifier/mofilo_nutrient_verifier.dart';

final food = FoodItem(
  id: 'abc123',
  name: 'Grilled Chicken Breast',
  servingSize: 100,
  servingUnit: 'g',
  protein: 31,
  carbs: 0,
  fat: 3.6,
  calories: 165,
);

final result = NutrientVerifier.verify(food);

if (result.pass) {
  print('Valid food (score: ${result.trustScore}/100)');
  // Safe to add to database
} else {
  print('Invalid food:');
  for (final reason in result.reasons) {
    print('  $reason');
  }
  // Reject submission
}

What This Package Does #

  • Validates nutrition data against physics-based limits (matter density, energy conservation)
  • Validates energy conservation using the Atwater formula (protein 4 kcal/g, carbs 4 kcal/g, fat 9 kcal/g, fiber 2 kcal/g, alcohol 7 kcal/g)
  • Detects acute toxicity risks for sodium, caffeine, vitamin A, potassium, iron, and vitamin D
  • Validates nutrient hierarchy (fiber ≤ carbs, sugars ≤ carbs, saturated fat ≤ total fat)
  • Validates liquid food physics (concentration limits for volume-based servings)
  • Rejects negative values for all nutrient fields
  • Rejects invalid serving counts (zero or negative)
  • Catches impossible cholesterol concentrations (garbage data detection)

What This Package Does NOT Do #

  • Does NOT verify if nutrition data is accurate — it only checks if it's physically possible. A food entry can pass all checks and still be wrong.
  • Does NOT detect missing ingredients — if a user forgets to log cooking oil, sauce, or toppings, the incomplete data will still pass
  • Does NOT detect measurement errors — wrong portion sizes, raw vs cooked confusion, and weight vs volume mix-ups are not caught
  • Does NOT handle sugar alcohols — foods with erythritol, xylitol, or other sugar alcohols may be incorrectly rejected because the validator counts all carbs at 4 kcal/g
  • Does NOT handle FDA rounding rules — legitimate nutrition labels that round values per FDA rules may trigger false rejections
  • Does NOT provide medical or dietary advice
  • Does NOT replace professional nutritional analysis
  • Does NOT validate all 42 nutrient fields — only 16 nutrients are actively validated (see Validation Coverage below). The remaining fields are stored but not checked.

Installation #

dependencies:
  mofilo_nutrient_verifier: ^1.0.2

Then run:

flutter pub get

Validation Pipeline #

The verifier runs 5 layers in order and fails fast on the first violation.

Layer 1: Input Validation #

  • Name required (min 2 characters)
  • Serving size must be > 0
  • Serving unit must be from the supported list
  • Serving count must be > 0
  • All nutrient values must be ≥ 0 (no negatives)

Layer 2: Volume Validation (liquids only) #

Applies when serving unit is ml, l, fl oz, cup, tbsp, or tsp.

  • Total macros ≤ 100g per 100ml
  • Fat ≤ 92g per 100ml (pure oil density)
  • Protein ≤ 50g per 100ml (solubility limit)
  • Carbs ≤ 60g per 100ml (sugar saturation)
  • Calories ≤ 850 kcal per 100ml

Layer 3: Physics Validation (weight-based only) #

Applies when serving unit can be converted to grams.

  • Matter density: total macros ≤ serving weight × 1.1
  • Concentration limits per 100g (protein ≤ 97g, fat ≤ 100g, carbs ≤ 100g, sodium ≤ 38,700mg, calories ≤ 900)
  • Hierarchy: fiber ≤ carbs, sugars ≤ carbs, added sugars ≤ total sugars, saturated fat ≤ total fat, trans fat ≤ total fat

Layer 4: Energy Validation #

  • Atwater formula check: stated calories must match calculated calories within ±25%
  • Special handling for zero-calorie and low-calorie foods

Layer 5: Safety Validation #

Nutrient Single Serving Limit Multi-Serving Limit
Sodium 3,000mg 10,000mg
Caffeine 400mg 1,000mg
Vitamin A 10,000mcg
Potassium 3,000mg 6,000mg
Iron 60mg (total) 60mg (total)
Vitamin D 250mcg (total) 250mcg (total)
Cholesterol ≤ 1,500mg per 100g (concentration check)

Validation Coverage #

Actively Validated (16 nutrients) #

Nutrient Checks Applied
calories physics concentration, energy conservation
protein physics density, physics concentration, energy
carbs physics density, physics concentration, energy, hierarchy
fat physics density, physics concentration, energy, hierarchy
dietaryFiber hierarchy (≤ carbs), energy (2 kcal/g)
totalSugars hierarchy (≤ carbs)
addedSugars hierarchy (≤ totalSugars)
saturatedFat hierarchy (≤ fat)
transFat hierarchy (≤ fat)
alcohol energy (7 kcal/g)
sodium physics concentration, safety toxicity
caffeine safety toxicity
vitaminA safety toxicity
potassium safety toxicity
iron safety toxicity
vitaminD safety toxicity
cholesterol safety concentration

Stored But NOT Validated (19 nutrients) #

These fields exist on FoodItem and are passed through, but the verifier does not check them for impossible values:

monounsaturatedFat, polyunsaturatedFat, vitaminC, vitaminE, vitaminK, thiamin, riboflavin, niacin, vitaminB6, folate, vitaminB12, biotin, pantothenicAcid, calcium, phosphorus, magnesium, chloride, zinc, selenium, copper, manganese, chromium, molybdenum

Negative values for all fields ARE rejected. But concentration limits and toxicity thresholds are not enforced for these nutrients.


Known Limitations #

These are documented trade-offs, not bugs:

  1. Count units use estimated weights — "1 piece" is assumed to be 150g, "1 bowl" is 400g, "1 bag" is 50g. These are rough estimates. A piece of gum (3g) and a piece of watermelon (300g) are both validated as 150g.

  2. Pint is treated as a weight unit — "pint" maps to 473g (ice cream use case) and skips volume validation. A pint of liquid with impossible nutrient density may pass.

  3. 25% calorie tolerance is generous — needed to accommodate cooking losses, resistant starch, and FDA label rounding, but it means calorie mismatches up to 25% will pass.

  4. Sugar alcohols are not handled — erythritol (0 kcal/g), xylitol (2.4 kcal/g), and other sugar alcohols are counted at 4 kcal/g like regular carbs. This causes false rejections for sugar-free products.

  5. FDA rounding rules are not implemented — FDA allows rounding <5 cal to 0, <0.5g fat to 0g, etc. Labels that use these rules may trigger energy validation failures.

  6. Volume foods get lower trust scores — volume-based servings (ml, cups) are inherently less precise than weight-based (g), so they receive a baseline trust score of 80 instead of 100.

  7. No detection of missing ingredients — the validator cannot know if a user forgot to log cooking oil, dressing, or toppings.

  8. No raw vs cooked detection — 100g of raw chicken (21g protein) and 100g of cooked chicken (31g protein) both pass validation.


Supported Serving Units #

Category Units
Weight g, kg, mg, oz, lb
Volume ml, l, fl oz, cup, tbsp, tsp, pint
Count serving, piece, slice, container, packet, bowl, plate, package, box, bag, sandwich, footlong, burger

THIS IS NOT MEDICAL ADVICE #

This software is a technical validation tool, NOT:

  • Medical advice or medical nutrition therapy
  • A substitute for healthcare professionals
  • Approved by FDA or any medical authority

DO NOT rely on this software alone if you have diabetes, kidney disease, heart disease, eating disorders, food allergies, or any medical condition.

ALWAYS consult a Registered Dietitian (RDN) or healthcare provider.

Accuracy Statement #

"Passing validation" does NOT mean the nutrition data is correct. It means the data is not physically impossible. A food entry with wrong but plausible values (e.g., forgetting to add cooking oil, using raw values for cooked food) will pass all checks.

This package catches approximately 5% of submissions that are clearly impossible. It does not catch the remaining inaccuracies that come from measurement errors, missing ingredients, or incorrect data entry.

Full legal terms: See DISCLAIMER.md and LICENSE


License #

MIT License - Copyright (c) 2025 Mofilo. See LICENSE for full terms.

Built by the Mofilo team

0
likes
150
points
104
downloads

Documentation

API reference

Publisher

verified publishermofilo.app

Weekly Downloads

Nutrition data validator for user-submitted food data. Validates using physics-based limits, energy conservation, and toxicity thresholds. Binary pass/fail with trust scoring.

Homepage
Repository (GitHub)

License

MIT (license)

Dependencies

flutter

More

Packages that depend on mofilo_nutrient_verifier