Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a valid AWSPhone? What test values can be used?

The docs say:

The AWSPhone scalar type represents a valid Phone Number. Phone numbers are serialized and deserialized as Strings. Phone numbers provided may be whitespace delimited or hyphenated. The number can specify a country code at the beginning but this is not required.

What determines whether a given string is a valid AWSPhone? In addition, is there any safe way to generate (possibly a large number of) AWSPhone test values that are guaranteed to be valid but assuredly are not in-use phone numbers?

like image 592
Thom Smith Avatar asked Nov 20 '25 02:11

Thom Smith


1 Answers

TLDR: You cannot tell the exact rules how the type AWSPhone is validated in AppSync. However, if a value passes the test of the regular expression /^\+?\d[\d\s-]+$/ or validation by libphonenumber-js, then it is likely to be accepted by AppSync.

In the latest AppSync Developer Guide (Oct 6, 2021 UTC), the description was updated to:

A phone number. This value is stored as a string. Phone numbers can contain either spaces or hyphens to separate digit groups. Phone numbers without a country code are assumed to be US/North American numbers adhering to the North American Numbering Plan (NANP).

This doesn't really tell exactly what AppSync expects. E.g. Must country code include + as prefix?

From AWS's public repositories on GitHub, there are hints:

  • amplify-js datastore util method for frontend validation:

    export const isAWSPhone = (val: string): boolean => {
       return !!/^\+?\d[\d\s-]+$/.exec(val);
    };
    
  • amplify-appsync-simulator for amplify CLI mock features:

    //...
    import { isValidNumber } from 'libphonenumber-js';
    //...
    const phoneValidator = (ast, options) => {
       //...
       let isValid = isValidNumber(value, country);
       //...
    }
    

Therefore, a value is likely to be accepted by AppSync if it passes the above regex test and validation by libphonenumber-js (or libphonenumber, assuming they work equivalently).

like image 177
Lacek Avatar answered Nov 22 '25 22:11

Lacek