All files / src/schemas UserSchema.js

100% Statements 3/3
100% Branches 0/0
100% Functions 0/0
100% Lines 3/3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166          1x                     1x                                                                                                                                                                                                                                                                                                       1x  
'use strict'
 
/**
 * Dependencies
 */
const {JSONSchema} = require('@trust/json-document')
 
/**
 * OpenID Connect User Schema
 *
 * TODO
 * - make it extensible via provider configuration/host implementation
 * - support pairwise identifier algorithm
 * - support language tags
 * - implement JSON Schema such that it doesn't completely suck
 */
const schema = new JSONSchema({
  type: 'object',
 
  // simple property names
  // which props belong in pattern props and which here?
  properties: {
    sub: { type: 'string' }
  },
 
  // NOTES:
  // Use patternProperties to support Section 5.2 Claims Languages and Scripts
  // and 5.5.2 Languages and Scripts for Individual Claims.
  //
  // See:
  //    - http://openid.net/specs/openid-connect-core-1_0.html#ClaimsLanguagesAndScripts
  //    - http://openid.net/specs/openid-connect-core-1_0.html#IndividualClaimsLanguages
  //    - https://tools.ietf.org/html/rfc5646
  //
  // Questions:
  //    - How to make language tags case-insensitive if claim names are case sensitive?
  //    - Should boolean/number values be properties rather than patternProperties?
  //    - Can we provide a simpler way of defining pattern properties via JSONSchema
  //      to make it easy on extenders of the schema?
  //
  patternProperties: {
 
    '^name': {
      type: 'string'
    },
 
    '^given_name(#-)?': {
      type: 'string'
    },
 
    '^middle_name': {
      type: 'string'
    },
 
    '^family_name': {
      type: 'string'
    },
 
    '^given_name': {
      type: 'string'
    },
 
    '^nickname': {
      type: 'string'
    },
 
    '^preferred_username': {
      type: 'string'
    },
 
    '^profile': {
      type: 'string',
      //format: 'url'
    },
 
    '^picture': {
      type: 'string',
      //format: 'url'
    },
 
    '^website': {
      type: 'string',
      //format: 'url'
    },
 
    '^email': {
      type: 'string',
      format: 'email'
    },
 
    '^email_verified': {
      type: 'boolean'
    },
 
    '^gender': {
      type: 'string'
    },
 
    '^birthdate': {
      type: 'string',
      // MOVE THIS REGEXP TO A CONSTANT OR OBJ SOMEWHERE AND TEST THE FUCK OUT OF IT
      //format: /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/
    },
 
    // TODO
    // valid values come from tz database
    // - http://www.twinsun.com/tz/tz-link.htm
    //
    // See also
    // - http://momentjs.com/timezone/docs/
    // - https://www.npmjs.com/package/timezone
    // - https://github.com/darkskyapp/tz-lookup COOL!!
    '^zoneinfo': {
      type: 'string'
    },
 
    //
    '^locale': {
      type: 'string',
      //format: TODO: regexp for Language Tag
    },
 
    '^phone_number': {
      type: 'string',
      //format: TODO: E.164 + RFC3966
    },
 
    '^phone_number_verified': {
      type: 'boolean'
    },
 
    '^address': {
      type: 'object',
      properties: {
        formatted: {
          type: 'string'
        },
        street_address: {
          type: 'string'
        },
        locality: {
          type: 'string'
        },
        region: {
          type: 'string'
        },
        postal_code: {
          type: 'string'
        },
        country: {
          type: 'string'
        }
      }
    },
 
    '^updated_at': {
      type: 'number'
    }
  }
})
 
/**
 * Export
 */
module.exports = schema