{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.prisme-donnees.fr/depenses-publiques/schemas/saisie.schema.json",
  "title": "Saisie d'un dispositif (une des deux saisies indépendantes)",
  "type": "object",
  "required": [
    "dispositif",
    "saisie",
    "saisi_par",
    "agent",
    "methode_extraction",
    "date",
    "valeurs"
  ],
  "additionalProperties": false,
  "properties": {
    "dispositif": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9._-]*$"
    },
    "saisie": {
      "type": "string",
      "enum": [
        "a",
        "b"
      ]
    },
    "saisi_par": {
      "type": "string",
      "enum": [
        "assistee",
        "personne"
      ]
    },
    "agent": {
      "type": "string",
      "minLength": 1
    },
    "methode_extraction": {
      "type": "string",
      "minLength": 1
    },
    "date": {
      "type": "string",
      "pattern": "^\\d{4}(-\\d{2}(-\\d{2})?)?$"
    },
    "valeurs": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "required": [
              "id",
              "type",
              "statut",
              "nature",
              "champs",
              "source"
            ],
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z0-9][a-z0-9._-]*$"
              },
              "type": {
                "type": "string",
                "enum": [
                  "financement",
                  "activite",
                  "resultat",
                  "objectif",
                  "effet",
                  "cout_tiers",
                  "fait"
                ]
              },
              "statut": {
                "const": "trouvee"
              },
              "nature": {
                "type": "string",
                "enum": [
                  "publiee",
                  "calculee",
                  "estimation_tiers"
                ]
              },
              "champs": {
                "type": "object"
              },
              "source": {
                "type": "object",
                "required": [
                  "document",
                  "page",
                  "extrait"
                ],
                "additionalProperties": false,
                "properties": {
                  "document": {
                    "type": "string",
                    "pattern": "^[a-z0-9][a-z0-9._-]*$"
                  },
                  "page": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "page du fichier PDF"
                  },
                  "page_imprimee": {
                    "type": [
                      "string",
                      "integer"
                    ]
                  },
                  "extrait": {
                    "type": "string",
                    "minLength": 8,
                    "description": "phrase exacte du document"
                  },
                  "tableau": {
                    "type": "string"
                  }
                }
              },
              "formule": {
                "type": "string"
              },
              "note": {
                "type": "string"
              }
            },
            "allOf": [
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "financement"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "financeur",
                        "instrument",
                        "exercice",
                        "stade",
                        "unite",
                        "montant",
                        "echelle",
                        "perimetre"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "financeur": {
                          "type": "string",
                          "enum": [
                            "etat",
                            "departements",
                            "regions",
                            "communes",
                            "securite_sociale",
                            "operateurs",
                            "france_competences",
                            "union_europeenne",
                            "autres"
                          ]
                        },
                        "instrument": {
                          "type": "string",
                          "enum": [
                            "credit_budgetaire",
                            "depense_fiscale",
                            "exoneration_compensee",
                            "exoneration_non_compensee",
                            "depense_securite_sociale",
                            "depense_collectivite",
                            "depense_operateur",
                            "fonds_europeens"
                          ]
                        },
                        "exercice": {
                          "type": "integer",
                          "minimum": 1990,
                          "maximum": 2100
                        },
                        "stade": {
                          "type": "string",
                          "enum": [
                            "prevu",
                            "vote",
                            "ouvert",
                            "execute",
                            "chiffrage"
                          ]
                        },
                        "unite": {
                          "type": "string",
                          "enum": [
                            "AE",
                            "CP",
                            "non_precisee",
                            "sans_objet"
                          ]
                        },
                        "montant": {
                          "type": "number"
                        },
                        "echelle": {
                          "type": "string",
                          "enum": [
                            "€",
                            "k€",
                            "M€",
                            "Md€"
                          ]
                        },
                        "perimetre": {
                          "type": "string",
                          "minLength": 1
                        },
                        "composante": {
                          "type": "string",
                          "minLength": 1
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "activite"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "mesure",
                        "periode",
                        "valeur",
                        "unite"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "mesure": {
                          "type": "string",
                          "minLength": 1
                        },
                        "definition": {
                          "type": "string"
                        },
                        "periode": {
                          "type": "string",
                          "minLength": 1
                        },
                        "valeur": {
                          "type": "number"
                        },
                        "unite": {
                          "type": "string",
                          "minLength": 1
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "resultat"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "indicateur",
                        "population",
                        "periode",
                        "valeur",
                        "unite"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "indicateur": {
                          "type": "string",
                          "minLength": 1
                        },
                        "population": {
                          "type": "string",
                          "minLength": 1
                        },
                        "periode": {
                          "type": "string",
                          "minLength": 1
                        },
                        "valeur": {
                          "type": "number"
                        },
                        "unite": {
                          "type": "string",
                          "minLength": 1
                        },
                        "cible": {
                          "type": [
                            "number",
                            "null"
                          ]
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "objectif"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "citation"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "citation": {
                          "type": "string",
                          "minLength": 20
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "effet"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "evaluation",
                        "critere",
                        "sens",
                        "unite",
                        "population",
                        "periode"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "evaluation": {
                          "type": "string",
                          "pattern": "^[a-z0-9][a-z0-9._-]*$"
                        },
                        "critere": {
                          "type": "string",
                          "pattern": "^[a-z0-9][a-z0-9._-]*$"
                        },
                        "sens": {
                          "type": "string",
                          "enum": [
                            "positif",
                            "nul",
                            "negatif",
                            "contraste",
                            "indetermine"
                          ]
                        },
                        "ampleur": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "unite": {
                          "type": "string",
                          "minLength": 1
                        },
                        "horizon_mois": {
                          "type": [
                            "number",
                            "null"
                          ],
                          "minimum": 0
                        },
                        "intervalle_bas": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "intervalle_haut": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "erreur_type": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "p_valeur": {
                          "type": [
                            "number",
                            "null"
                          ],
                          "minimum": 0,
                          "maximum": 1
                        },
                        "population": {
                          "type": "string",
                          "minLength": 1
                        },
                        "periode": {
                          "type": "string",
                          "minLength": 1
                        },
                        "comparaison": {
                          "type": "string",
                          "minLength": 1
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "cout_tiers"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "annee",
                        "unite",
                        "perimetre"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "annee": {
                          "type": "integer"
                        },
                        "montant": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "montant_bas": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "montant_haut": {
                          "type": [
                            "number",
                            "null"
                          ]
                        },
                        "unite": {
                          "type": "string",
                          "minLength": 1
                        },
                        "perimetre": {
                          "type": "string",
                          "minLength": 1
                        },
                        "auteur": {
                          "type": "string",
                          "minLength": 1
                        },
                        "evaluation": {
                          "type": "string",
                          "pattern": "^[a-z0-9][a-z0-9._-]*$"
                        }
                      }
                    }
                  }
                }
              },
              {
                "if": {
                  "properties": {
                    "type": {
                      "const": "fait"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "champs": {
                      "type": "object",
                      "required": [
                        "valeur",
                        "unite",
                        "definition"
                      ],
                      "additionalProperties": false,
                      "properties": {
                        "valeur": {
                          "type": [
                            "number",
                            "string"
                          ]
                        },
                        "unite": {
                          "type": "string"
                        },
                        "definition": {
                          "type": "string",
                          "minLength": 1
                        }
                      }
                    }
                  }
                }
              }
            ]
          },
          {
            "type": "object",
            "required": [
              "id",
              "type",
              "statut",
              "note"
            ],
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z0-9][a-z0-9._-]*$"
              },
              "type": {
                "type": "string",
                "enum": [
                  "financement",
                  "activite",
                  "resultat",
                  "objectif",
                  "effet",
                  "cout_tiers",
                  "fait"
                ]
              },
              "statut": {
                "const": "introuvable"
              },
              "note": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        ]
      }
    }
  }
}
