forked from opentripplanner/otp-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Add lat/lng validation): lat/lng validation done in core-utils/ma…
…p; added support for isRequired
- Loading branch information
1 parent
0cad5d1
commit 8659b67
Showing
4 changed files
with
58 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ReactPropTypeLocationNames } from "react"; | ||
import { isValidLatLng } from "./map"; | ||
|
||
/** | ||
* Utility function to help create chained validators | ||
* per https://www.ian-thomas.net/custom-proptype-validation-with-react/ | ||
* @param {*} validator The validator to use. | ||
*/ | ||
export function createChainableTypeChecker(validator) { | ||
function checkType(isRequired, props, propName, componentName, location) { | ||
componentName = componentName || "ANONYMOUS"; | ||
if (props[propName] == null) { | ||
if (isRequired) { | ||
const locationName = ReactPropTypeLocationNames[location]; | ||
return new Error( | ||
`Required '${locationName}/${propName}' was not specified in '${componentName}'.` | ||
); | ||
} | ||
return null; | ||
} | ||
return validator(props, propName, componentName, location); | ||
} | ||
|
||
const chainedCheckType = checkType.bind(null, false); | ||
chainedCheckType.isRequired = checkType.bind(null, true); | ||
|
||
return chainedCheckType; | ||
} | ||
|
||
export const LatlngPropType = createChainableTypeChecker((props, propName) => { | ||
// Source: https://reactjs.org/docs/typechecking-with-proptypes.html#react.proptypes | ||
if (!isValidLatLng(props[propName])) { | ||
return new Error(`${propName} needs to be a [lat, lng] array`); | ||
} | ||
return null; | ||
}); |