Model Schema

Includes the attribute and model schemas; can validate models using the Ajv JSON-Schema validator.

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/4.1.2/ajv.min.js"></script>

JavaScript

const schema = {
    type: 'object',
    properties: {
        users: {
            type: 'array',
            items: {
                type: 'object',
                properties: {
                    userReference: { type: 'string' }, 
                    phone: {
                        type: 'string'
                        , pattern: "^\+[0-9]{9,12}$" // If I remove this line, the model is seen as valid (no errors)
                    }
                }
            }
        }
    },
    required: ['users'],
    errorMessage: { _: "One or more of the fields in the 'legacy' data path are incorrect."    }
};

const schemaSample = {
    "users": [
      {
				"phone": "+25512345678", // should be valid
        "userReference": "AAA"
      },
      {
        "phone": "+5255 abc 12345678", // should be invalid
        "userReference": "BBB"
      }
    ]
  };

var ajv = Ajv();
ajv.addSchema(schema, 'schema');

var valid = ajv.validate('schema', schemaSample);
if (valid) {
  console.log('Model is valid!');
} else {
  console.log('Model is invalid!');
}