{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aistory-archive.pages.dev/standards/sensory.schema.json",
  "title": "S-Code / ISO-Taste Sensory Standards Family 1.0",
  "description": "Public exchange contract for S-Code sensory events and ISO-Taste reference/interoperability data, including conformance, personal calibration, provenance, and independent rights surfaces.",
  "type": "object",
  "required": [
    "identifier",
    "version",
    "registry_version",
    "system_role",
    "measurement_status",
    "provenance",
    "rights_consent",
    "conformance"
  ],
  "properties": {
    "identifier": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "const": "1.0"
    },
    "system_role": {
      "enum": [
        "s-code",
        "iso-taste",
        "mapped-profile"
      ]
    },
    "measurement_status": {
      "$ref": "#/$defs/measurementStatus"
    },
    "reference_class": {
      "enum": [
        "R0",
        "AR",
        "HR",
        "PR"
      ]
    },
    "s_code": {
      "type": "object",
      "properties": {
        "standard_version": {
          "const": "1.0"
        },
        "T": {
          "$ref": "#/$defs/eventList"
        },
        "O": {
          "$ref": "#/$defs/eventList"
        },
        "X": {
          "$ref": "#/$defs/eventList"
        },
        "M": {
          "$ref": "#/$defs/eventList"
        },
        "P": {
          "$ref": "#/$defs/eventList"
        },
        "H": {
          "$ref": "#/$defs/eventList"
        },
        "A": {
          "$ref": "#/$defs/eventList"
        },
        "V": {
          "$ref": "#/$defs/eventList"
        },
        "U": {
          "$ref": "#/$defs/calibrationProfile"
        }
      },
      "additionalProperties": false,
      "required": [
        "standard_version"
      ],
      "anyOf": [
        {
          "required": [
            "T"
          ]
        },
        {
          "required": [
            "O"
          ]
        },
        {
          "required": [
            "X"
          ]
        },
        {
          "required": [
            "M"
          ]
        },
        {
          "required": [
            "P"
          ]
        },
        {
          "required": [
            "H"
          ]
        },
        {
          "required": [
            "A"
          ]
        },
        {
          "required": [
            "V"
          ]
        },
        {
          "required": [
            "U"
          ]
        }
      ]
    },
    "iso_taste": {
      "type": "object",
      "properties": {
        "standard_version": {
          "const": "1.0"
        },
        "public_code": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9]*-[0-9]{3,}$"
        },
        "structural_code": {
          "type": "string",
          "pattern": "^ISO-TASTE:[a-z0-9]+(?:\\.[a-z0-9-]+)+@[0-9]+\\.[0-9]+$"
        },
        "reference_profile": {
          "$ref": "#/$defs/referenceProfile"
        },
        "cultural_regional_context": {
          "type": [
            "string",
            "object"
          ]
        },
        "calibration_notes": {
          "type": "string"
        },
        "code_registry_ref": {
          "const": "SENSORY-REGISTRY@1.0"
        }
      },
      "additionalProperties": false,
      "required": [
        "standard_version",
        "code_registry_ref",
        "reference_profile"
      ],
      "anyOf": [
        {
          "required": [
            "public_code"
          ]
        },
        {
          "required": [
            "structural_code"
          ]
        }
      ]
    },
    "mapping": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/mapping"
      },
      "minItems": 1
    },
    "residual_extensions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "channel",
          "reason"
        ],
        "properties": {
          "channel": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    },
    "provenance": {
      "$ref": "#/$defs/provenance"
    },
    "rights_consent": {
      "$ref": "#/$defs/rightsConsent"
    },
    "conformance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema",
        "reference",
        "experiential"
      ],
      "properties": {
        "schema": {
          "$ref": "#/$defs/conformanceAssessment"
        },
        "reference": {
          "$ref": "#/$defs/conformanceAssessment"
        },
        "experiential": {
          "$ref": "#/$defs/conformanceAssessment"
        }
      }
    },
    "registry_version": {
      "const": "1.0"
    },
    "migration": {
      "$ref": "#/$defs/migrationRecord"
    }
  },
  "$defs": {
    "measurementStatus": {
      "enum": [
        "measured",
        "estimated",
        "inferred"
      ]
    },
    "eventList": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "descriptor",
          "measurement_status"
        ],
        "properties": {
          "descriptor": {
            "type": "string"
          },
          "value": {
            "type": [
              "number",
              "string"
            ]
          },
          "unit": {
            "enum": [
              "1",
              "%",
              "Pa",
              "kPa",
              "N",
              "°C",
              "s",
              "ms",
              "Hz",
              "dB",
              "Pa·s"
            ]
          },
          "time_context": {
            "enum": [
              "pre-entry",
              "entry",
              "initial-contact",
              "chewing",
              "chewing-to-aftertaste",
              "swallow",
              "post-swallow",
              "aftertaste",
              "thermal-transition",
              "release",
              "decay",
              "serving",
              "custom"
            ]
          },
          "measurement_status": {
            "$ref": "#/$defs/measurementStatus"
          },
          "confidence": {
            "type": "number",
            "minimum": 0,
            "maximum": 1
          },
          "provenance": {
            "$ref": "#/$defs/provenance"
          },
          "descriptor_ref": {
            "type": "string",
            "pattern": "^SENSORY-DESC:[TOXMPHAV]:[a-z0-9]+(?:-[a-z0-9]+)*@1\\.0$"
          },
          "custom_time_context": {
            "type": "string",
            "minLength": 1
          }
        },
        "additionalProperties": false,
        "allOf": [
          {
            "if": {
              "properties": {
                "time_context": {
                  "const": "custom"
                }
              },
              "required": [
                "time_context"
              ]
            },
            "then": {
              "required": [
                "custom_time_context"
              ]
            }
          },
          {
            "if": {
              "required": [
                "custom_time_context"
              ]
            },
            "then": {
              "properties": {
                "time_context": {
                  "const": "custom"
                }
              },
              "required": [
                "time_context"
              ]
            }
          }
        ]
      }
    },
    "referenceProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "reference_id",
        "name",
        "version",
        "source_id",
        "measurement_status",
        "values",
        "provenance"
      ],
      "properties": {
        "reference_id": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "minLength": 1
        },
        "source_id": {
          "type": "string",
          "minLength": 1
        },
        "measurement_status": {
          "$ref": "#/$defs/measurementStatus"
        },
        "cultural_regional_context": {
          "type": [
            "string",
            "object"
          ]
        },
        "tolerance_profile_ref": {
          "type": "string"
        },
        "values": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/referenceValue"
          }
        },
        "provenance": {
          "$ref": "#/$defs/provenance"
        }
      }
    },
    "mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "from",
        "to",
        "relationship"
      ],
      "properties": {
        "from": {
          "enum": [
            "S-Code:T",
            "S-Code:O",
            "S-Code:X",
            "S-Code:M",
            "S-Code:H",
            "S-Code:A",
            "S-Code:U"
          ]
        },
        "to": {
          "enum": [
            "ISO-Taste:Taste_Target",
            "ISO-Taste:Aroma_Family/Release_Curve",
            "ISO-Taste:Texture_Target",
            "ISO-Taste:Mouthfeel_Target",
            "ISO-Taste:Thermal_Target",
            "ISO-Taste:Sound_Axis",
            "ISO-Taste:Calibration_Profile"
          ]
        },
        "relationship": {
          "const": "semantic_mapping"
        },
        "note": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:T"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Taste_Target"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:O"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Aroma_Family/Release_Curve"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:X"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Texture_Target"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:M"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Mouthfeel_Target"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:H"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Thermal_Target"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:A"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Sound_Axis"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "from": {
                "const": "S-Code:U"
              }
            },
            "required": [
              "from"
            ]
          },
          "then": {
            "properties": {
              "to": {
                "const": "ISO-Taste:Calibration_Profile"
              }
            }
          }
        }
      ]
    },
    "provenance": {
      "type": "object",
      "properties": {
        "source_basis": {
          "type": "string",
          "minLength": 1
        },
        "source_id": {
          "type": "string",
          "minLength": 1
        },
        "method": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false,
      "required": [
        "source_basis",
        "source_id",
        "method"
      ]
    },
    "rightsConsent": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "measurement",
        "preference_learning",
        "model_training",
        "commercial_reuse",
        "reconstruction",
        "induction"
      ],
      "properties": {
        "measurement": {
          "$ref": "#/$defs/permission"
        },
        "preference_learning": {
          "$ref": "#/$defs/permission"
        },
        "model_training": {
          "$ref": "#/$defs/permission"
        },
        "commercial_reuse": {
          "$ref": "#/$defs/permission"
        },
        "reconstruction": {
          "$ref": "#/$defs/permission"
        },
        "induction": {
          "$ref": "#/$defs/permission"
        },
        "purpose": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "restrictions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "revocation_status": {
          "enum": [
            "active",
            "revoked",
            "not_applicable",
            "unknown"
          ]
        }
      }
    },
    "permission": {
      "enum": [
        "allow",
        "deny",
        "conditional",
        "unknown",
        "not_applicable"
      ]
    },
    "calibrationProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "profile_id",
        "profile_version",
        "profile_scope",
        "measurement_status",
        "calibration_basis",
        "provenance"
      ],
      "properties": {
        "profile_id": {
          "type": "string",
          "minLength": 1
        },
        "profile_version": {
          "type": "string",
          "minLength": 1
        },
        "profile_scope": {
          "enum": [
            "personal",
            "population_prior",
            "synthetic"
          ]
        },
        "measurement_status": {
          "$ref": "#/$defs/measurementStatus"
        },
        "calibration_basis": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          }
        },
        "adjustments": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "family",
              "qualitative_adjustment"
            ],
            "properties": {
              "family": {
                "enum": [
                  "T",
                  "O",
                  "X",
                  "M",
                  "P",
                  "H",
                  "A",
                  "V"
                ]
              },
              "descriptor": {
                "type": "string"
              },
              "qualitative_adjustment": {
                "type": "string",
                "minLength": 1
              },
              "parameter_ref": {
                "type": "string"
              },
              "confidence": {
                "type": "number",
                "minimum": 0,
                "maximum": 1
              },
              "provenance": {
                "$ref": "#/$defs/provenance"
              },
              "descriptor_ref": {
                "type": "string",
                "pattern": "^SENSORY-DESC:[TOXMPHAV]:[a-z0-9]+(?:-[a-z0-9]+)*@1\\.0$"
              }
            }
          }
        },
        "provenance": {
          "$ref": "#/$defs/provenance"
        }
      }
    },
    "referenceValue": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "descriptor",
        "value",
        "measurement_status",
        "provenance"
      ],
      "properties": {
        "descriptor": {
          "type": "string",
          "minLength": 1
        },
        "value": {
          "type": [
            "number",
            "string"
          ]
        },
        "unit": {
          "enum": [
            "1",
            "%",
            "Pa",
            "kPa",
            "N",
            "°C",
            "s",
            "ms",
            "Hz",
            "dB",
            "Pa·s"
          ]
        },
        "measurement_status": {
          "$ref": "#/$defs/measurementStatus"
        },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "provenance": {
          "$ref": "#/$defs/provenance"
        },
        "descriptor_ref": {
          "type": "string",
          "pattern": "^SENSORY-DESC:[TOXMPHAV]:[a-z0-9]+(?:-[a-z0-9]+)*@1\\.0$"
        }
      }
    },
    "conformanceAssessment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status"
      ],
      "properties": {
        "status": {
          "enum": [
            "pass",
            "fail",
            "partial",
            "not_evaluated"
          ]
        },
        "basis_ref": {
          "type": "string"
        },
        "tolerance_profile_ref": {
          "type": "string"
        },
        "calibration_profile_ref": {
          "type": "string"
        },
        "measurement_status": {
          "$ref": "#/$defs/measurementStatus"
        },
        "provenance": {
          "$ref": "#/$defs/provenance"
        },
        "note": {
          "type": "string"
        }
      }
    },
    "migrationRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "migration_id",
        "source_version",
        "target_version",
        "registry_version",
        "status",
        "preserved_fields",
        "dropped_fields"
      ],
      "properties": {
        "migration_id": {
          "type": "string",
          "pattern": "^SENSORY-MIGRATION-[A-Za-z0-9._-]+$"
        },
        "source_version": {
          "type": "string",
          "minLength": 1
        },
        "target_version": {
          "type": "string",
          "minLength": 1
        },
        "registry_version": {
          "const": "1.0"
        },
        "status": {
          "enum": [
            "none_required",
            "lossless",
            "lossy"
          ]
        },
        "preserved_fields": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "dropped_fields": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "note": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "lossy"
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "properties": {
              "dropped_fields": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "enum": [
                  "none_required",
                  "lossless"
                ]
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "properties": {
              "dropped_fields": {
                "maxItems": 0
              }
            }
          }
        }
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "system_role": {
            "const": "s-code"
          }
        },
        "required": [
          "system_role"
        ]
      },
      "then": {
        "required": [
          "s_code"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "system_role": {
            "const": "iso-taste"
          }
        },
        "required": [
          "system_role"
        ]
      },
      "then": {
        "required": [
          "iso_taste"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "system_role": {
            "const": "mapped-profile"
          }
        },
        "required": [
          "system_role"
        ]
      },
      "then": {
        "required": [
          "s_code",
          "iso_taste",
          "mapping"
        ]
      }
    }
  ],
  "additionalProperties": false
}
