@kravc/schema 2.7.5 → 2.8.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/README.md +19 -14
  2. package/dist/CredentialFactory.d.ts +345 -0
  3. package/dist/CredentialFactory.d.ts.map +1 -0
  4. package/dist/CredentialFactory.js +381 -0
  5. package/dist/CredentialFactory.js.map +1 -0
  6. package/dist/Schema.d.ts +448 -0
  7. package/dist/Schema.d.ts.map +1 -0
  8. package/dist/Schema.js +506 -0
  9. package/dist/Schema.js.map +1 -0
  10. package/dist/ValidationError.d.ts +70 -0
  11. package/dist/ValidationError.d.ts.map +1 -0
  12. package/dist/ValidationError.js +78 -0
  13. package/dist/ValidationError.js.map +1 -0
  14. package/dist/Validator.d.ts +483 -0
  15. package/dist/Validator.d.ts.map +1 -0
  16. package/dist/Validator.js +570 -0
  17. package/dist/Validator.js.map +1 -0
  18. package/dist/helpers/JsonSchema.d.ts +99 -0
  19. package/dist/helpers/JsonSchema.d.ts.map +1 -0
  20. package/dist/helpers/JsonSchema.js +3 -0
  21. package/dist/helpers/JsonSchema.js.map +1 -0
  22. package/dist/helpers/cleanupAttributes.d.ts +34 -0
  23. package/dist/helpers/cleanupAttributes.d.ts.map +1 -0
  24. package/dist/helpers/cleanupAttributes.js +113 -0
  25. package/dist/helpers/cleanupAttributes.js.map +1 -0
  26. package/dist/helpers/cleanupNulls.d.ts +27 -0
  27. package/dist/helpers/cleanupNulls.d.ts.map +1 -0
  28. package/dist/helpers/cleanupNulls.js +96 -0
  29. package/dist/helpers/cleanupNulls.js.map +1 -0
  30. package/dist/helpers/getReferenceIds.d.ts +169 -0
  31. package/dist/helpers/getReferenceIds.d.ts.map +1 -0
  32. package/dist/helpers/getReferenceIds.js +241 -0
  33. package/dist/helpers/getReferenceIds.js.map +1 -0
  34. package/dist/helpers/got.d.ts +60 -0
  35. package/dist/helpers/got.d.ts.map +1 -0
  36. package/dist/helpers/got.js +72 -0
  37. package/dist/helpers/got.js.map +1 -0
  38. package/dist/helpers/mapObjectProperties.d.ts +150 -0
  39. package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
  40. package/dist/helpers/mapObjectProperties.js +229 -0
  41. package/dist/helpers/mapObjectProperties.js.map +1 -0
  42. package/dist/helpers/normalizeAttributes.d.ts +213 -0
  43. package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
  44. package/dist/helpers/normalizeAttributes.js +243 -0
  45. package/dist/helpers/normalizeAttributes.js.map +1 -0
  46. package/dist/helpers/normalizeProperties.d.ts +168 -0
  47. package/dist/helpers/normalizeProperties.d.ts.map +1 -0
  48. package/dist/helpers/normalizeProperties.js +223 -0
  49. package/dist/helpers/normalizeProperties.js.map +1 -0
  50. package/dist/helpers/normalizeRequired.d.ts +159 -0
  51. package/dist/helpers/normalizeRequired.d.ts.map +1 -0
  52. package/dist/helpers/normalizeRequired.js +206 -0
  53. package/dist/helpers/normalizeRequired.js.map +1 -0
  54. package/dist/helpers/normalizeType.d.ts +81 -0
  55. package/dist/helpers/normalizeType.d.ts.map +1 -0
  56. package/dist/helpers/normalizeType.js +210 -0
  57. package/dist/helpers/normalizeType.js.map +1 -0
  58. package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
  59. package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
  60. package/dist/helpers/nullifyEmptyValues.js +191 -0
  61. package/dist/helpers/nullifyEmptyValues.js.map +1 -0
  62. package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
  63. package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
  64. package/dist/helpers/removeRequiredAndDefault.js +138 -0
  65. package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
  66. package/dist/helpers/validateId.d.ts +39 -0
  67. package/dist/helpers/validateId.d.ts.map +1 -0
  68. package/dist/helpers/validateId.js +51 -0
  69. package/dist/helpers/validateId.js.map +1 -0
  70. package/dist/index.d.ts +7 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +17 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/ld/documentLoader.d.ts +8 -0
  75. package/dist/ld/documentLoader.d.ts.map +1 -0
  76. package/dist/ld/documentLoader.js +24 -0
  77. package/dist/ld/documentLoader.js.map +1 -0
  78. package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
  79. package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
  80. package/dist/ld/getLinkedDataAttributeType.js +32 -0
  81. package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
  82. package/dist/ld/getLinkedDataContext.d.ts +19 -0
  83. package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
  84. package/dist/ld/getLinkedDataContext.js +50 -0
  85. package/dist/ld/getLinkedDataContext.js.map +1 -0
  86. package/eslint.config.mjs +32 -52
  87. package/examples/credentials/createAccountCredential.ts +27 -0
  88. package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
  89. package/examples/index.ts +7 -0
  90. package/examples/schemas/FavoriteItemSchema.ts +27 -0
  91. package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
  92. package/examples/schemas/PreferencesSchema.ts +29 -0
  93. package/examples/schemas/ProfileSchema.ts +91 -0
  94. package/examples/schemas/Status.yaml +3 -0
  95. package/examples/schemas/StatusSchema.ts +12 -0
  96. package/jest.config.mjs +5 -0
  97. package/package.json +28 -21
  98. package/src/CredentialFactory.ts +392 -0
  99. package/src/Schema.ts +583 -0
  100. package/src/ValidationError.ts +90 -0
  101. package/src/Validator.ts +603 -0
  102. package/src/__tests__/CredentialFactory.test.ts +588 -0
  103. package/src/__tests__/Schema.test.ts +371 -0
  104. package/src/__tests__/ValidationError.test.ts +235 -0
  105. package/src/__tests__/Validator.test.ts +787 -0
  106. package/src/helpers/JsonSchema.ts +119 -0
  107. package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
  108. package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
  109. package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
  110. package/src/helpers/__tests__/got.test.ts +193 -0
  111. package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
  112. package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
  113. package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
  114. package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
  115. package/src/helpers/__tests__/normalizeType.test.ts +772 -0
  116. package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
  117. package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
  118. package/src/helpers/__tests__/validateId.test.ts +118 -0
  119. package/src/helpers/cleanupAttributes.ts +151 -0
  120. package/src/helpers/cleanupNulls.ts +106 -0
  121. package/src/helpers/getReferenceIds.ts +273 -0
  122. package/src/helpers/got.ts +73 -0
  123. package/src/helpers/mapObjectProperties.ts +272 -0
  124. package/src/helpers/normalizeAttributes.ts +247 -0
  125. package/src/helpers/normalizeProperties.ts +249 -0
  126. package/src/helpers/normalizeRequired.ts +233 -0
  127. package/src/helpers/normalizeType.ts +235 -0
  128. package/src/helpers/nullifyEmptyValues.ts +207 -0
  129. package/src/helpers/removeRequiredAndDefault.ts +151 -0
  130. package/src/helpers/validateId.ts +53 -0
  131. package/src/index.ts +13 -0
  132. package/src/ld/__tests__/documentLoader.test.ts +57 -0
  133. package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
  134. package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
  135. package/src/ld/documentLoader.ts +28 -0
  136. package/src/ld/getLinkedDataAttributeType.ts +46 -0
  137. package/src/ld/getLinkedDataContext.ts +80 -0
  138. package/tsconfig.json +27 -0
  139. package/types/credentials-context.d.ts +14 -0
  140. package/types/security-context.d.ts +6 -0
  141. package/examples/Status.yaml +0 -3
  142. package/examples/createAccountCredential.js +0 -27
  143. package/examples/createMineSweeperScoreCredential.js +0 -63
  144. package/examples/index.js +0 -9
  145. package/src/CredentialFactory.js +0 -67
  146. package/src/CredentialFactory.spec.js +0 -131
  147. package/src/Schema.js +0 -104
  148. package/src/Schema.spec.js +0 -172
  149. package/src/ValidationError.js +0 -31
  150. package/src/Validator.js +0 -128
  151. package/src/Validator.spec.js +0 -355
  152. package/src/helpers/cleanupAttributes.js +0 -71
  153. package/src/helpers/cleanupNulls.js +0 -42
  154. package/src/helpers/getReferenceIds.js +0 -71
  155. package/src/helpers/mapObject.js +0 -65
  156. package/src/helpers/normalizeAttributes.js +0 -28
  157. package/src/helpers/normalizeProperties.js +0 -61
  158. package/src/helpers/normalizeRequired.js +0 -37
  159. package/src/helpers/normalizeType.js +0 -41
  160. package/src/helpers/nullifyEmptyValues.js +0 -57
  161. package/src/helpers/removeRequiredAndDefault.js +0 -30
  162. package/src/helpers/validateId.js +0 -19
  163. package/src/index.d.ts +0 -25
  164. package/src/index.js +0 -8
  165. package/src/ld/documentLoader.js +0 -25
  166. package/src/ld/documentLoader.spec.js +0 -12
  167. package/src/ld/getLinkedDataContext.js +0 -63
  168. package/src/ld/getLinkedDataType.js +0 -38
  169. /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
  170. /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
@@ -1,71 +0,0 @@
1
- 'use strict'
2
-
3
- const { isUndefined } = require('lodash')
4
-
5
- const cleanupAttributes = (object, jsonSchema, schemasMap) => {
6
- const { id, enum: isEnum } = jsonSchema
7
-
8
- if (isEnum) { return }
9
-
10
-
11
- for (const fieldName in object) {
12
- const property = jsonSchema.properties[fieldName]
13
- const isPropertyUndefined = isUndefined(property)
14
-
15
- if (isPropertyUndefined) {
16
- delete object[fieldName]
17
-
18
- } else {
19
- const { $ref: refSchemaId, properties, type } = property
20
-
21
- const isArray = type === 'array'
22
- const isObject = type === 'object'
23
- const isReference = !isUndefined(refSchemaId)
24
-
25
- if (isReference) {
26
- const refJsonSchema = schemasMap[refSchemaId]
27
-
28
- cleanupAttributes(object[fieldName], refJsonSchema, schemasMap)
29
-
30
- } else if (isObject) {
31
- const nestedJsonSchema = {
32
- id: `${id}.${fieldName}.properties`,
33
- type: 'object',
34
- properties
35
- }
36
-
37
- cleanupAttributes(object[fieldName], nestedJsonSchema, schemasMap)
38
-
39
- } else if (isArray) {
40
- const { items } = property
41
- const { $ref: itemRefSchemaId, properties: itemProperties } = items
42
-
43
- const array = object[fieldName]
44
- const isItemObject = !!itemProperties
45
- const isItemReference = !!itemRefSchemaId
46
-
47
- let itemJsonSchema
48
-
49
- if (isItemReference) {
50
- itemJsonSchema = schemasMap[itemRefSchemaId]
51
-
52
- } else if (isItemObject) {
53
- itemJsonSchema = {
54
- id: `${id}.${fieldName}.item`,
55
- type: 'object',
56
- properties: itemProperties
57
- }
58
-
59
- }
60
-
61
- if (itemJsonSchema) {
62
- for (const item of array) {
63
- cleanupAttributes(item, itemJsonSchema, schemasMap)
64
- }
65
- }
66
- }
67
- }
68
- }
69
- }
70
-
71
- module.exports = cleanupAttributes
@@ -1,42 +0,0 @@
1
- 'use strict'
2
-
3
- const { isArray } = Array
4
- const { isObject, cloneDeep } = require('lodash')
5
-
6
- const cleanupNulls = object => {
7
- if (!isObject(object)) {
8
- return
9
- }
10
-
11
- for (const key in object) {
12
- const value = object[key]
13
-
14
- if (isArray(value)) {
15
- for (const item of value) {
16
- cleanupNulls(item)
17
- }
18
-
19
- continue
20
- }
21
-
22
- if (isObject(value)) {
23
- cleanupNulls(value)
24
-
25
- continue
26
- }
27
-
28
- const isNull = value === null
29
-
30
- if (isNull) {
31
- delete object[key]
32
- }
33
- }
34
- }
35
-
36
- module.exports = input => {
37
- const object = cloneDeep(input)
38
-
39
- cleanupNulls(object)
40
-
41
- return object
42
- }
@@ -1,71 +0,0 @@
1
- 'use strict'
2
-
3
- const { isUndefined, uniq } = require('lodash')
4
- const Schema = require('../Schema')
5
-
6
- const getReferenceIds = (schema, schemasMap) => {
7
- const getSchema = id => schemasMap[id]
8
-
9
- let referenceIds = []
10
-
11
- const { jsonSchema } = schema
12
- const { id, enum: isEnum } = jsonSchema
13
-
14
- if (isEnum) { return [] }
15
-
16
- for (const propertyName in jsonSchema.properties) {
17
- const property = jsonSchema.properties[propertyName]
18
-
19
- const { $ref: refSchemaId, properties, items } = property
20
-
21
- const isArray = property.type === 'array'
22
- const isObject = property.type === 'object'
23
- const isReference = !isUndefined(refSchemaId)
24
-
25
- if (isReference) {
26
- const refJsonSchema = getSchema(refSchemaId, `${id}.${propertyName}.$ref`)
27
- const nestedReferenceIds = getReferenceIds(refJsonSchema, schemasMap)
28
-
29
- referenceIds = [ ...referenceIds, refSchemaId, ...nestedReferenceIds ]
30
- continue
31
-
32
- }
33
-
34
- if (isObject) {
35
- const nestedSchema = new Schema(properties, `${id}.${propertyName}.properties`)
36
- const nestedReferenceIds = getReferenceIds(nestedSchema, schemasMap)
37
-
38
- referenceIds = [ ...referenceIds, ...nestedReferenceIds ]
39
- continue
40
- }
41
-
42
- if (!isArray) {
43
- continue
44
- }
45
-
46
- const itemProperties = items.properties
47
- const itemRefSchemaId = items.$ref
48
-
49
- let itemJsonSchema
50
-
51
- if (itemRefSchemaId) {
52
- itemJsonSchema = getSchema(itemRefSchemaId, `${id}.${propertyName}.items.$ref`)
53
- const nestedReferenceIds = getReferenceIds(itemJsonSchema, schemasMap)
54
-
55
- referenceIds = [ ...referenceIds, itemRefSchemaId, ...nestedReferenceIds ]
56
- continue
57
- }
58
-
59
- if (itemProperties) {
60
- const itemSchema = new Schema(itemProperties, `${id}.${propertyName}.items.properties`)
61
- const itemReferenceIds = getReferenceIds(itemSchema, schemasMap)
62
-
63
- referenceIds = [ ...referenceIds, ...itemReferenceIds ]
64
- continue
65
- }
66
- }
67
-
68
- return uniq(referenceIds)
69
- }
70
-
71
- module.exports = getReferenceIds
@@ -1,65 +0,0 @@
1
- 'use strict'
2
-
3
- const { isUndefined } = require('lodash')
4
-
5
- const mapObject = (object, jsonSchema, schemasMap, callback) => {
6
- const { id, enum: isEnum } = jsonSchema
7
-
8
- if (isEnum) { return }
9
-
10
- for (const propertyName in jsonSchema.properties) {
11
- const property = jsonSchema.properties[propertyName]
12
-
13
- callback(propertyName, property, object)
14
-
15
- const { $ref: refSchemaId, properties, items } = property
16
-
17
- const value = object[propertyName]
18
- const isArray = property.type === 'array'
19
- const isObject = property.type === 'object'
20
- const isReference = !isUndefined(refSchemaId)
21
- const isValueDefined = !isUndefined(value)
22
-
23
- if (isValueDefined) {
24
- if (isReference) {
25
- const refJsonSchema = schemasMap[refSchemaId]
26
-
27
- mapObject(value, refJsonSchema, schemasMap, callback)
28
-
29
- } else if (isObject) {
30
- const nestedJsonSchema = {
31
- id: `${id}.${propertyName}.properties`,
32
- type: 'object',
33
- properties
34
- }
35
-
36
- mapObject(value, nestedJsonSchema, schemasMap, callback)
37
-
38
- } else if (isArray) {
39
- const itemProperties = items.properties
40
- const itemRefSchemaId = items.$ref
41
-
42
- let itemJsonSchema
43
-
44
- if (itemRefSchemaId) {
45
- itemJsonSchema = schemasMap[itemRefSchemaId]
46
-
47
- } else if (itemProperties) {
48
- itemJsonSchema = {
49
- id: `${id}.${propertyName}.items.properties`,
50
- type: 'object',
51
- properties: itemProperties
52
- }
53
- }
54
-
55
- if (itemJsonSchema) {
56
- for (const valueItem of value) {
57
- mapObject(valueItem, itemJsonSchema, schemasMap, callback)
58
- }
59
- }
60
- }
61
- }
62
- }
63
- }
64
-
65
- module.exports = mapObject
@@ -1,28 +0,0 @@
1
- 'use strict'
2
-
3
- const mapObject = require('./mapObject')
4
- const normalizeType = require('./normalizeType')
5
- const { isUndefined } = require('lodash')
6
-
7
- const normalizeAttributes = (object, jsonSchema, jsonSchemasMap) => {
8
- const callback = (propertyName, propertySchema, object) => {
9
- const value = object[propertyName]
10
-
11
- const { type, default: defaultValue } = propertySchema
12
-
13
- const hasDefaultValue = !isUndefined(defaultValue)
14
- const isValueDefined = !isUndefined(value)
15
-
16
- if (hasDefaultValue && !isValueDefined) {
17
- object[propertyName] = defaultValue
18
- }
19
-
20
- if (type && isValueDefined) {
21
- object[propertyName] = normalizeType(type, value)
22
- }
23
- }
24
-
25
- mapObject(object, jsonSchema, jsonSchemasMap, callback)
26
- }
27
-
28
- module.exports = normalizeAttributes
@@ -1,61 +0,0 @@
1
- 'use strict'
2
-
3
- const { isUndefined } = require('lodash')
4
-
5
- const normalizeProperties = properties => {
6
- const { enum: isEnum } = properties
7
-
8
- if (isEnum) {
9
- properties.type = properties.type || 'string'
10
- return
11
- }
12
-
13
- for (const name in properties) {
14
- const property = properties[name]
15
-
16
- const { type: hasType, $ref: isRef, items: hasItems, properties: hasProperties } = property
17
-
18
- if (!isRef) {
19
- if (!hasType) {
20
- if (hasProperties) {
21
- property.type = 'object'
22
-
23
- } else if (hasItems) {
24
- property.type = 'array'
25
-
26
- } else {
27
- property.type = 'string'
28
-
29
- }
30
- }
31
-
32
- const isArray = property.type === 'array'
33
- const isObject = property.type === 'object'
34
-
35
- if (isObject) {
36
- if (!hasProperties) {
37
- property.properties = {}
38
- }
39
-
40
- normalizeProperties(property.properties)
41
- }
42
-
43
- if (isArray) {
44
- if (hasItems) {
45
- const isItemObject = !isUndefined(property.items.properties)
46
-
47
- if (isItemObject) {
48
- property.items.type = 'object'
49
- normalizeProperties(property.items.properties)
50
- }
51
-
52
- } else {
53
- property.items = { type: 'string' }
54
-
55
- }
56
- }
57
- }
58
- }
59
- }
60
-
61
- module.exports = normalizeProperties
@@ -1,37 +0,0 @@
1
- 'use strict'
2
-
3
- const normalizeRequired = jsonSchema => {
4
- const { properties } = jsonSchema
5
-
6
- if (!properties) { return }
7
-
8
- const required = []
9
- for (const name in properties) {
10
- const property = properties[name]
11
-
12
- if (property.required) {
13
- property['x-required'] = true
14
- required.push(name)
15
- }
16
-
17
- delete properties[name].required
18
-
19
- const isObject = property.type === 'object'
20
- const isArray = property.type === 'array'
21
-
22
- if (isObject) {
23
- normalizeRequired(property)
24
- }
25
-
26
- if (isArray) {
27
- const { items: itemJsonSchema } = property
28
- normalizeRequired(itemJsonSchema)
29
- }
30
- }
31
-
32
- if (required.length > 0) {
33
- jsonSchema.required = required
34
- }
35
- }
36
-
37
- module.exports = normalizeRequired
@@ -1,41 +0,0 @@
1
- 'use strict'
2
-
3
- const BOOLEAN_STRING_TRUE_VALUES = ['yes', 'true', '1']
4
- const BOOLEAN_STRING_FALSE_VALUES = ['no', 'false', '0']
5
-
6
- const normalizeType = (type, value) => {
7
- let normalizedValue = value
8
-
9
- const isNumber = type === 'integer' || type === 'number'
10
- const isBoolean = type === 'boolean'
11
-
12
- if (isNumber) {
13
- normalizedValue = Number(value) || value
14
- }
15
-
16
- if (isBoolean) {
17
- const isNumberValue = typeof value === 'number'
18
- const isStringValue = typeof value === 'string'
19
-
20
- const shouldConvertValue = isNumberValue || isStringValue
21
-
22
- if (shouldConvertValue) {
23
- if (isNumberValue) {
24
- normalizedValue = Boolean(value)
25
- }
26
-
27
- if (isStringValue) {
28
- const isTrue = BOOLEAN_STRING_TRUE_VALUES.includes(value.toLowerCase())
29
- const isFalse = BOOLEAN_STRING_FALSE_VALUES.includes(value.toLowerCase())
30
-
31
- if (isTrue || isFalse) {
32
- normalizedValue = isTrue ? true : false
33
- }
34
- }
35
- }
36
- }
37
-
38
- return normalizedValue
39
- }
40
-
41
- module.exports = normalizeType
@@ -1,57 +0,0 @@
1
- 'use strict'
2
-
3
- const { get, set } = require('lodash')
4
- const { schemaSymbol, jsonSymbol } = require('z-schema')
5
-
6
- const FORMAT_ERROR_CODES = [
7
- 'PATTERN',
8
- 'ENUM_MISMATCH',
9
- 'INVALID_FORMAT'
10
- ]
11
-
12
- const EMPTY_VALUES = [
13
- '',
14
- ]
15
-
16
- const nullifyEmptyValues = (object, validationErrors) => {
17
- const objectJson = JSON.stringify(object)
18
- const result = JSON.parse(objectJson)
19
-
20
- const otherValidationErrors = []
21
-
22
- for (const error of validationErrors) {
23
- const { code } = error
24
-
25
- const isAttributeRequired = error[schemaSymbol]['x-required'] === true
26
- const isFormatError = FORMAT_ERROR_CODES.includes(code)
27
-
28
- if (isAttributeRequired) {
29
- otherValidationErrors.push(error)
30
- continue
31
- }
32
-
33
- if (!isFormatError) {
34
- otherValidationErrors.push(error)
35
- continue
36
- }
37
-
38
- const { path: pathString } = error
39
- const path = pathString.replace('#/', '').split('/')
40
-
41
- const json = error[jsonSymbol]
42
- const value = get(json, path)
43
-
44
- const isEmptyValue = EMPTY_VALUES.includes(value)
45
-
46
- if (!isEmptyValue) {
47
- otherValidationErrors.push(error)
48
- continue
49
- }
50
-
51
- set(result, path, null)
52
- }
53
-
54
- return [ result, otherValidationErrors ]
55
- }
56
-
57
- module.exports = nullifyEmptyValues
@@ -1,30 +0,0 @@
1
- 'use strict'
2
-
3
- const removeRequiredAndDefault = jsonSchema => {
4
- const { properties } = jsonSchema
5
-
6
- if (!properties) { return }
7
-
8
- for (const name in properties) {
9
- const property = properties[name]
10
-
11
- delete property.required
12
- delete property.default
13
-
14
- const isObject = property.type === 'object'
15
- const isArray = property.type === 'array'
16
-
17
- if (isObject) {
18
- removeRequiredAndDefault(property)
19
- }
20
-
21
- if (isArray) {
22
- const { items: itemsJsonSchema } = property
23
- removeRequiredAndDefault(itemsJsonSchema)
24
- }
25
- }
26
-
27
- return { properties }
28
- }
29
-
30
- module.exports = removeRequiredAndDefault
@@ -1,19 +0,0 @@
1
- 'use strict'
2
-
3
- const { isURL } = require('validator')
4
-
5
- const validateId = (name, value) => {
6
- if (!value) {
7
- throw new Error(`Parameter "${name}" is required`)
8
- }
9
-
10
- const isURI = value.toLowerCase().startsWith('did:') || isURL(value)
11
-
12
- if (isURI) {
13
- return
14
- }
15
-
16
- throw new Error(`Parameter "${name}" must be a URL, received: "${value}"`)
17
- }
18
-
19
- module.exports = validateId
package/src/index.d.ts DELETED
@@ -1,25 +0,0 @@
1
- declare type SchemaAttribute = object;
2
- export declare type SchemaAttributes = Record<string, SchemaAttribute>;
3
- declare type Enum = { enum: string[] };
4
- declare type Source = Enum | SchemaAttributes;
5
-
6
- export declare class Schema {
7
- constructor(
8
- source: Source,
9
- id: string,
10
- url?: string
11
- )
12
-
13
- get id(): string;
14
- get source(): Source;
15
-
16
- only(propertyNames: string[], id?: string): Schema;
17
- wrap(propertyName: string, options?: Record<string, any>, id?: string): Schema;
18
- extend(properties: Record<string, any>, id?: string): Schema;
19
- }
20
-
21
- export declare class Validator {
22
- constructor(
23
- schemas: Schema[]
24
- )
25
- }
package/src/index.js DELETED
@@ -1,8 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports = {
4
- Schema: require('./Schema'),
5
- Validator: require('./Validator'),
6
- documentLoader: require('./ld/documentLoader'),
7
- CredentialFactory: require('./CredentialFactory')
8
- }
@@ -1,25 +0,0 @@
1
- 'use strict'
2
-
3
- const { contexts: securityContextsMap } = require('security-context')
4
- const { contexts: credentialsContextsMap } = require('credentials-context')
5
-
6
- const CONTEXTS = new Map([ ...securityContextsMap, ...credentialsContextsMap ])
7
-
8
- const documentLoader = documentUrl => {
9
- const contextUrl = null
10
- const [ url ] = documentUrl.split('#')
11
-
12
- const document = CONTEXTS.get(url)
13
-
14
- if (!document) {
15
- throw new Error(`Custom context "${documentUrl}" is not supported`)
16
- }
17
-
18
- return {
19
- document,
20
- contextUrl,
21
- documentUrl
22
- }
23
- }
24
-
25
- module.exports = documentLoader
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- const { expect } = require('chai')
4
- const documentLoader = require('./documentLoader')
5
-
6
- describe('documentLoader(documentUrl)', () => {
7
- it('throws error if context not found', () => {
8
- expect(
9
- () => documentLoader('https://example.com')
10
- ).to.throw('Custom context "https://example.com')
11
- })
12
- })
@@ -1,63 +0,0 @@
1
- 'use strict'
2
-
3
- const getLinkedDataType = require('./getLinkedDataType')
4
-
5
- const SCHEMA_ORG_URI = 'https://schema.org/'
6
-
7
- const PROTECTED_PROPERTIES = [
8
- 'id',
9
- 'type',
10
- 'schema'
11
- ]
12
-
13
- // TODO: Add support for embedded object, array and enum.
14
- const getLinkedDataContext = (properties, vocabUri) => {
15
- const context = {}
16
-
17
- for (const key in properties) {
18
- const schema = properties[key]
19
-
20
- const isProtected = PROTECTED_PROPERTIES.includes(key) || key.startsWith('@')
21
-
22
- if (isProtected) {
23
- continue
24
- }
25
-
26
- const { $ref } = schema
27
-
28
- if ($ref) {
29
- context[key] = { '@id': key }
30
- continue
31
- }
32
-
33
- context[key] = { '@id': key }
34
-
35
- const linkedDataType = getLinkedDataType(schema)
36
-
37
- if (linkedDataType) {
38
- context[key]['@type'] = linkedDataType
39
- }
40
- }
41
-
42
- const vocab =
43
- vocabUri.endsWith('/') || vocabUri.endsWith('#') ? vocabUri : `${vocabUri}#`
44
-
45
- const contextHeader = {
46
- '@vocab': vocab,
47
- '@version': 1.1,
48
- '@protected': true
49
- }
50
-
51
- const isSchemaOrgDomain = vocab === SCHEMA_ORG_URI
52
-
53
- if (!isSchemaOrgDomain) {
54
- contextHeader.schema = SCHEMA_ORG_URI
55
- }
56
-
57
- return {
58
- ...contextHeader,
59
- ...context
60
- }
61
- }
62
-
63
- module.exports = getLinkedDataContext