Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only accept "address" result from google API - validate address_components before using it #5854

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/components/AddressSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ const AddressSearch = (props) => {
.value();
};

const validateAddressComponents = (addressComponents) => {
if (!addressComponents) {
return false;
}
if (!_.some(addressComponents, component => _.includes(component.types, 'street_number'))) {
// Missing Street number
return false;
}
if (_.some(addressComponents, component => _.includes(component.types, 'post_box'))) {
// Reject PO box
return false;
}
return true;
};

const saveLocationDetails = (details) => {
if (details.address_components) {
if (validateAddressComponents(details.address_components)) {
// Gather the values from the Google details
const streetNumber = getAddressComponent(details, 'street_number', 'long_name');
const streetName = getAddressComponent(details, 'route', 'long_name');
Expand All @@ -61,6 +76,12 @@ const AddressSearch = (props) => {
props.onChangeText('addressCity', city);
props.onChangeText('addressState', state);
props.onChangeText('addressZipCode', zipCode);
} else {
// Clear the values associated to the address, so our validations catch the problem
props.onChangeText('addressStreet', null);
props.onChangeText('addressCity', null);
props.onChangeText('addressState', null);
props.onChangeText('addressZipCode', null);
}
};

Expand All @@ -74,6 +95,8 @@ const AddressSearch = (props) => {
query={{
key: 'AIzaSyC4axhhXtpiS-WozJEsmlL3Kg3kXucbZus',
language: props.preferredLocale,
types: 'address',
components: 'country:us',
}}
requestUrl={{
useOnPlatform: 'web',
Expand Down