{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://antroponimia.pt/estatisticas/schema.json",
  "title": "Biblioteca editorial de popularidade de nomes em Portugal",
  "description": "Análises reproduzíveis limitadas às linhas dos Top 20 anuais publicados pelo Instituto dos Registos e do Notariado.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "$schema",
    "schemaVersion",
    "catalogSchemaVersion",
    "dataUpdatedAt",
    "period",
    "scope",
    "methodology",
    "sources",
    "annualAnalyses",
    "decadeAnalyses"
  ],
  "properties": {
    "$schema": {
      "const": "https://antroponimia.pt/estatisticas/schema.json"
    },
    "schemaVersion": {
      "const": "1.0.0"
    },
    "catalogSchemaVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
    },
    "dataUpdatedAt": {
      "$ref": "#/$defs/date"
    },
    "period": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "firstYear",
        "lastYear",
        "yearsAvailable",
        "yearCount"
      ],
      "properties": {
        "firstYear": {
          "$ref": "#/$defs/year"
        },
        "lastYear": {
          "$ref": "#/$defs/year"
        },
        "yearsAvailable": {
          "$ref": "#/$defs/years"
        },
        "yearCount": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "geography",
        "units",
        "coverage",
        "publishedRowCount",
        "populationDenominatorAvailable"
      ],
      "properties": {
        "geography": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "units": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "coverage": {
          "const": "top-20-only"
        },
        "publishedRowCount": {
          "type": "integer",
          "minimum": 1
        },
        "populationDenominatorAvailable": {
          "const": false
        }
      }
    },
    "methodology": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "ranking",
        "movement",
        "entryExit",
        "persistence",
        "decade",
        "absence"
      ],
      "properties": {
        "ranking": {
          "type": "string",
          "minLength": 1
        },
        "movement": {
          "type": "string",
          "minLength": 1
        },
        "entryExit": {
          "type": "string",
          "minLength": 1
        },
        "persistence": {
          "type": "string",
          "minLength": 1
        },
        "decade": {
          "type": "string",
          "minLength": 1
        },
        "absence": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "sources": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/source"
      },
      "minItems": 1
    },
    "annualAnalyses": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/annualAnalysis"
      },
      "minItems": 1
    },
    "decadeAnalyses": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/decadeAnalysis"
      },
      "minItems": 1
    }
  },
  "$defs": {
    "date": {
      "type": "string",
      "format": "date",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
    },
    "year": {
      "type": "integer",
      "minimum": 1900,
      "maximum": 9999
    },
    "years": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/year"
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "publisher",
        "title",
        "url",
        "license",
        "accessedAt",
        "coverage"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "publisher": {
          "type": "string",
          "minLength": 1
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https?://"
        },
        "license": {
          "type": "string",
          "minLength": 1
        },
        "licenseUrl": {
          "type": "string",
          "format": "uri",
          "pattern": "^https?://"
        },
        "accessedAt": {
          "$ref": "#/$defs/date"
        },
        "coverage": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "rankedRow": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "nameId",
        "name",
        "rank",
        "count",
        "sourceId"
      ],
      "properties": {
        "nameId": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "rank": {
          "type": "integer",
          "minimum": 1
        },
        "count": {
          "type": "integer",
          "minimum": 0
        },
        "sourceId": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "movement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "nameId",
        "name",
        "previousRank",
        "currentRank",
        "rankChange",
        "direction",
        "previousCount",
        "currentCount",
        "countChange"
      ],
      "properties": {
        "nameId": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "previousRank": {
          "type": "integer",
          "minimum": 1
        },
        "currentRank": {
          "type": "integer",
          "minimum": 1
        },
        "rankChange": {
          "type": "integer"
        },
        "direction": {
          "enum": [
            "subida",
            "descida",
            "estável"
          ]
        },
        "previousCount": {
          "type": "integer",
          "minimum": 0
        },
        "currentCount": {
          "type": "integer",
          "minimum": 0
        },
        "countChange": {
          "type": "integer"
        }
      }
    },
    "comparison": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "previousYear",
        "currentYear",
        "basis",
        "retainedCount",
        "enteredTop20",
        "leftTop20",
        "movements"
      ],
      "properties": {
        "previousYear": {
          "$ref": "#/$defs/year"
        },
        "currentYear": {
          "$ref": "#/$defs/year"
        },
        "basis": {
          "const": "consecutive-published-top-20"
        },
        "retainedCount": {
          "type": "integer",
          "minimum": 0
        },
        "enteredTop20": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/rankedRow"
          }
        },
        "leftTop20": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/rankedRow"
          }
        },
        "movements": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/movement"
          }
        }
      }
    },
    "annualSegment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "year",
        "segment",
        "segmentKey",
        "label",
        "sourceIds",
        "scopes",
        "units",
        "rowCount",
        "publishedCountSum",
        "leader",
        "rows",
        "comparison"
      ],
      "properties": {
        "year": {
          "$ref": "#/$defs/year"
        },
        "segment": {
          "type": "string",
          "minLength": 1
        },
        "segmentKey": {
          "type": "string",
          "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
        },
        "label": {
          "type": "string",
          "minLength": 1
        },
        "sourceIds": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "scopes": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "units": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "rowCount": {
          "type": "integer",
          "minimum": 1
        },
        "publishedCountSum": {
          "type": "integer",
          "minimum": 0
        },
        "leader": {
          "$ref": "#/$defs/rankedRow"
        },
        "rows": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/rankedRow"
          },
          "minItems": 1
        },
        "comparison": {
          "$ref": "#/$defs/comparison"
        }
      }
    },
    "annualAnalysis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "year",
        "segmentCount",
        "rowCount",
        "segments"
      ],
      "properties": {
        "year": {
          "$ref": "#/$defs/year"
        },
        "segmentCount": {
          "type": "integer",
          "minimum": 1
        },
        "rowCount": {
          "type": "integer",
          "minimum": 1
        },
        "segments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/annualSegment"
          },
          "minItems": 1
        }
      }
    },
    "persistence": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "nameId",
        "name",
        "appearanceCount",
        "availableYearCount",
        "appearanceShareOfAvailableRankings",
        "presentInEveryAvailableYear",
        "longestConsecutiveRun",
        "firstObservedYear",
        "lastObservedYear",
        "bestRank",
        "publishedCountSum",
        "series"
      ],
      "properties": {
        "nameId": {
          "type": "string",
          "minLength": 1
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "appearanceCount": {
          "type": "integer",
          "minimum": 1
        },
        "availableYearCount": {
          "type": "integer",
          "minimum": 1
        },
        "appearanceShareOfAvailableRankings": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "presentInEveryAvailableYear": {
          "type": "boolean"
        },
        "longestConsecutiveRun": {
          "type": "integer",
          "minimum": 1
        },
        "firstObservedYear": {
          "$ref": "#/$defs/year"
        },
        "lastObservedYear": {
          "$ref": "#/$defs/year"
        },
        "bestRank": {
          "type": "integer",
          "minimum": 1
        },
        "publishedCountSum": {
          "type": "integer",
          "minimum": 0
        },
        "series": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "year",
              "rank",
              "count"
            ],
            "properties": {
              "year": {
                "$ref": "#/$defs/year"
              },
              "rank": {
                "type": "integer",
                "minimum": 1
              },
              "count": {
                "type": "integer",
                "minimum": 0
              }
            }
          }
        }
      }
    },
    "decadeSegment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "segment",
        "segmentKey",
        "label",
        "sourceIds",
        "rowCount",
        "publishedCountSum",
        "transitionCount",
        "enteredTop20EventCount",
        "leftTop20EventCount",
        "transitions",
        "persistence"
      ],
      "properties": {
        "segment": {
          "type": "string",
          "minLength": 1
        },
        "segmentKey": {
          "type": "string",
          "minLength": 1
        },
        "label": {
          "type": "string",
          "minLength": 1
        },
        "sourceIds": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "rowCount": {
          "type": "integer",
          "minimum": 1
        },
        "publishedCountSum": {
          "type": "integer",
          "minimum": 0
        },
        "transitionCount": {
          "type": "integer",
          "minimum": 0
        },
        "enteredTop20EventCount": {
          "type": "integer",
          "minimum": 0
        },
        "leftTop20EventCount": {
          "type": "integer",
          "minimum": 0
        },
        "transitions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/comparison"
          }
        },
        "persistence": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/persistence"
          },
          "minItems": 1
        }
      }
    },
    "decadeAnalysis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "decade",
        "periodStart",
        "periodEnd",
        "yearsAvailable",
        "yearCount",
        "calendarYearCount",
        "completeDecade",
        "coverage",
        "segments"
      ],
      "properties": {
        "decade": {
          "$ref": "#/$defs/year"
        },
        "periodStart": {
          "$ref": "#/$defs/year"
        },
        "periodEnd": {
          "$ref": "#/$defs/year"
        },
        "yearsAvailable": {
          "$ref": "#/$defs/years"
        },
        "yearCount": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10
        },
        "calendarYearCount": {
          "const": 10
        },
        "completeDecade": {
          "type": "boolean"
        },
        "coverage": {
          "const": "partial-published-top-20-window"
        },
        "segments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/decadeSegment"
          },
          "minItems": 1
        }
      }
    }
  }
}