Skip to content

Instantly share code, notes, and snippets.

@pckilgore
Last active May 8, 2019 21:31
Show Gist options
  • Save pckilgore/dab2f2c10af06d5072a4a58b551731f2 to your computer and use it in GitHub Desktop.
Save pckilgore/dab2f2c10af06d5072a4a58b551731f2 to your computer and use it in GitHub Desktop.
AppSync `AWSDate` scalar validation regex (JavaScript / ECMAScript)
/**
* LICENSE: MIT
*
* References / attribution:
* date portion:
* - https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
* timezone portion:
* - https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html
*/
const isAWSDate = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?(Z|[+-](?:2[0-3]|[01][0-9])(?::?(?:[0-5][0-9]))?)?$/;
export default date => isAWSDate.test(date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment