什么是有效的 AWSPhone?可以使用哪些测试值?

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

docs 说:

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.

什么决定给定的字符串是否有效 AWSPhone?此外,是否有任何安全的方法来生成(可能是大量的)AWSPhone 保证有效但肯定不会在使用中的 phone 数字的测试值?

我会看看流行的 google 库来处理 phone 数字

https://github.com/google/libphonenumber

TLDR:您无法告诉确切的规则如何在 AppSync 中验证类型 AWSPhone。但是,如果一个值通过了正则表达式 /^\+?\d[\d\s-]+$/ 的测试或 libphonenumber-js 的验证,则它很可能被 AppSync 接受。

在最近的AppSync Developer Guide(UTC 2021 年 10 月 6 日)中,描述更新为:

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).

这并不能准确说明 AppSync 的期望。例如。国家代码必须包含 + 作为前缀吗?

来自 GitHub 上 AWS 的 public 存储库,有提示:

  • amplify-js datastore util method 用于前端验证:

    export const isAWSPhone = (val: string): boolean => {
       return !!/^\+?\d[\d\s-]+$/.exec(val);
    };
    
  • amplify-appsync-simulator 用于扩展 CLI 模拟功能:

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

因此,如果 AppSync 通过上述正则表达式测试和 libphonenumber-js 验证(或 libphonenumber,假设它们等效工作),则该值可能会被 AppSync 接受.