# 5. Location Validation

When we receive an alert from an IoT service provider, RapidSOS validates the civic location according to the[ location schema](https://rapidsos.portal.swaggerhub.com/alerts/default/alert-management-api-v-1-1-0) . However, a given agency may require different formats for addresses to be dispatchable. To account for these situations, RapidSOS provides a mechanism for CADs to validate civic addresses in the agency's local GIS layers and request changes to an address **before a real incident occurs** to save time during a true emergency. This process follows the following flow:

## 5.1 Subscribe to location.prevalidation events
---
Subscribe to [location.prevalidation](https://rapidsos.portal.swaggerhub.com/alerts/default/websocket-events-api-v-1-1-1) events through the [WebSocket](https://rapidsos.portal.swaggerhub.com/alerts/default/notification-subscription). When an IoT service provider sends addresses for validation to RapidSOS, a formatted version of this request will be sent to the CAD through the following event type for subscribed WebSockets. 

```json lineNumbers
{
    "event_type": "location.prevalidation",
    "body": {
        "location_id": "4842-4f45-483hr488-190e",
        "timestamp": 1281110270922,
        "civic_location": {
            "name": "RapidSOS HQ",
            "street_1": "123 Main Street",
            "street_2": "Apt. 626",
            "city": "Los Angeles",
            "state": "CA",
            "country": "USA",
            "zip_code": "11375"
        }
    },
    "timestamp": 1581110270922
}
```

## 5.2 Validate the Civic Location
---
When a [location.prevalidation](https://rapidsos.portal.swaggerhub.com/alerts/default/websocket-events-api-v-1-1-1) event is received, validate that `civic_location` against the local GIS and confirm that there is enough information to allow for dispatch of a future alert. 


## 5.3 Return Validation Results
---
Immediately upon receipt of an address validation message, return the result of the validation to RapidSOS. Please provide as much information as possible to improve the dispatching experience if an alert occurs at the given address in the future. 

### Valid Civic Location
Confirm that the provided address geocodes using local agency GIS and confirm that the location geocodes against local GIS to a single dispatchable address. If the address is valid for dispatch, return the "valid" format of the [location.prevalidation_response](https://rapidsos.portal.swaggerhub.com/alerts/default/cad-events-api-v-1-0-0) event to the [Events API](https://rapidsos.portal.swaggerhub.com/alerts/default/cad-events-api-v-1-0-0)

```json lineNumbers
{
    "event": "location.prevalidation_response",
    "body": {
        "location_id": "7489344a483d424aace10fbe86a759a1",
        "geodetic": {
            "latitude": 45.522699,
            "longitude": -122.673383,
            "uncertainty_radius": 7.5
        },
        "full_address": "22 SW 3rd Ave, Portland, OR 97204",
        "error": {}
    },
    "timestamp": 1581110270922
}
```

### Invalid Civic Location
If a civic location fails local validation, return an invalid response with one of the following response shapes. Options include **Invalid Format**, **Not Unique**, **Not Covered**, and **Other**. 

#### Invalid Format
Use this error detail when one or more of the provided civic location fields fails validation. This example shows an invalid format for the `street_1` field.

```json lineNumbers
{
  "event": "location.prevalidation_response",
  "body": {
    "location_id": "7489344a483d424aace10fbe86a759a1",
    "geodetic": {},
    "full_address": null,
    "error": {
      "detail": "INVALID_FORMAT",
      "invalid_fields": [
        {
          "street_1": "invalid_street_format"
        }
      ]
    }
  },
  "timestamp": 1581110270922
}
```

#### Address Not Unique
If a given address does not have enough information to be uniquely identified, such as an apartment building without a unit, this response should be used.

```json lineNumbers
{
    "event": "location.prevalidation_response",
    "body": {
        "location_id": "7489344a483d424aace10fbe86a759a1",
        "geodetic": {},
        "full_address": null,
        "error": {
            "detail": "NOT_UNIQUE",
            "invalid_fields": []
        }
    },
    "timestamp": 1581110270922
}
```


#### Address Not Covered
Use this response when receiving an otherwise valid addresses that are outside of the agency's jurisdiction. This should be rare. 

```json lineNumbers
{
    "event": "location.prevalidation_response",
    "body": {
        "location_id": "7489344a483d424aace10fbe86a759a1",
        "geodetic": {},
        "full_address": null,
        "error": {
            "detail": "NOT_COVERED",
            "invalid_fields": []
        }
    },
    "timestamp": 1581110270922
}
```

#### Other Error 
The address fails validation but not due to one of the specified reasons. 

```json lineNumbers
{
    "event": "location.prevalidation_response",
    "body": {
        "location_id": "7489344a483d424aace10fbe86a759a1",
        "geodetic": {},
        "full_address": null,
        "error": {
            "detail": "OTHER",
            "invalid_fields": []
        }
    },
    "timestamp": 1581110270922
}

```
