{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/bettermenu/json-schema/bettermenu-record-reference-food-view-request-schema.json",
  "title": "RecordReferenceFoodViewRequest",
  "description": "A food the member opened, or a portion change in an open food.",
  "x-generated": "2026-11-04",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/bettermenu-studio-api-openapi.yml#/components/schemas/RecordReferenceFoodViewRequest",
  "properties": {
    "food_id": {
      "type": "string",
      "maxLength": 128,
      "minLength": 1,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:\\-]*$",
      "title": "Food Id",
      "description": "Identifier of the food in the food database."
    },
    "name": {
      "type": "string",
      "maxLength": 200,
      "minLength": 1,
      "title": "Name",
      "description": "The food's name as shown to the member."
    },
    "brand": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 200,
          "minLength": 1
        },
        {
          "type": "null"
        }
      ],
      "title": "Brand",
      "description": "The brand as shown, for a branded food."
    },
    "source": {
      "$ref": "#/$defs/ReferenceFoodSource",
      "description": "`generic` for a general food, `branded` for a branded product."
    },
    "per_100g": {
      "$ref": "#/$defs/ReferenceFoodPer100g",
      "description": "The headline nutrients per 100 g, as shown to the member."
    },
    "data_version": {
      "type": "string",
      "maxLength": 64,
      "minLength": 1,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:+\\-]*$",
      "title": "Data Version",
      "description": "Version of the food data the member was shown."
    },
    "action": {
      "$ref": "#/$defs/ReferenceFoodViewAction",
      "description": "`open` when the member opened the food. `portion` when they changed the portion of a food they already opened."
    },
    "opened_from": {
      "$ref": "#/$defs/ReferenceFoodOpenedFrom",
      "description": "The tab the food was opened from: `search`, `recent` or `saved`."
    },
    "portion": {
      "anyOf": [
        {
          "$ref": "#/$defs/ReferenceFoodPortion"
        },
        {
          "type": "null"
        }
      ],
      "description": "The portion shown. Required for `portion`. On `open` it becomes the food's last portion; when omitted the last portion is kept."
    },
    "result_position": {
      "anyOf": [
        {
          "type": "integer",
          "maximum": 10000.0,
          "minimum": 1.0
        },
        {
          "type": "null"
        }
      ],
      "title": "Result Position",
      "description": "Where the food appeared in the search results, starting at 1 (at most 10000). Only for `open` from `search`."
    },
    "query": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 200,
          "minLength": 1
        },
        {
          "type": "null"
        }
      ],
      "title": "Query",
      "description": "The search text that found the food. Kept in the member's own view history only. Only for `open` from `search`."
    }
  },
  "additionalProperties": false,
  "type": "object",
  "required": [
    "food_id",
    "name",
    "source",
    "per_100g",
    "data_version",
    "action",
    "opened_from"
  ],
  "$defs": {
    "ReferenceFoodOpenedFrom": {
      "type": "string",
      "enum": [
        "search",
        "recent",
        "saved"
      ],
      "title": "ReferenceFoodOpenedFrom",
      "description": "The tab the member opened the food from."
    },
    "ReferenceFoodPer100g": {
      "properties": {
        "calories": {
          "anyOf": [
            {
              "type": "number",
              "maximum": 100000.0,
              "minimum": 0.0
            },
            {
              "type": "null"
            }
          ],
          "title": "Calories",
          "description": "Energy in kilocalories per 100 g. Zero, or from 0.000001 to 100000."
        },
        "total_fat": {
          "anyOf": [
            {
              "type": "number",
              "maximum": 100000.0,
              "minimum": 0.0
            },
            {
              "type": "null"
            }
          ],
          "title": "Total Fat",
          "description": "Total fat in grams per 100 g. Zero, or from 0.000001 to 100000."
        },
        "total_carbohydrate": {
          "anyOf": [
            {
              "type": "number",
              "maximum": 100000.0,
              "minimum": 0.0
            },
            {
              "type": "null"
            }
          ],
          "title": "Total Carbohydrate",
          "description": "Total carbohydrate in grams per 100 g. Zero, or from 0.000001 to 100000."
        },
        "protein": {
          "anyOf": [
            {
              "type": "number",
              "maximum": 100000.0,
              "minimum": 0.0
            },
            {
              "type": "null"
            }
          ],
          "title": "Protein",
          "description": "Protein in grams per 100 g. Zero, or from 0.000001 to 100000."
        }
      },
      "additionalProperties": false,
      "type": "object",
      "title": "ReferenceFoodPer100g",
      "description": "Headline nutrients per 100 g of the food. Each is `null` when unknown."
    },
    "ReferenceFoodPortion": {
      "properties": {
        "label": {
          "type": "string",
          "maxLength": 100,
          "minLength": 1,
          "title": "Label",
          "description": "The portion as shown, e.g. `1 cup` or `100 g`."
        },
        "grams": {
          "type": "number",
          "maximum": 100000.0,
          "minimum": 0.001,
          "title": "Grams",
          "description": "The weight of the portion in grams, from 0.001 to 100000."
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "label",
        "grams"
      ],
      "title": "ReferenceFoodPortion",
      "description": "An amount of the food, as shown to the member."
    },
    "ReferenceFoodSource": {
      "type": "string",
      "enum": [
        "generic",
        "branded"
      ],
      "title": "ReferenceFoodSource",
      "description": "Which part of the food database a food comes from."
    },
    "ReferenceFoodViewAction": {
      "type": "string",
      "enum": [
        "open",
        "portion"
      ],
      "title": "ReferenceFoodViewAction",
      "description": "What the member did that produced a view event."
    }
  }
}