@kravc/schema 2.7.6 → 2.8.0-alpha.1

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 (176) 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/createSchemasMap.d.ts +67 -0
  31. package/dist/helpers/createSchemasMap.d.ts.map +1 -0
  32. package/dist/helpers/createSchemasMap.js +200 -0
  33. package/dist/helpers/createSchemasMap.js.map +1 -0
  34. package/dist/helpers/getReferenceIds.d.ts +169 -0
  35. package/dist/helpers/getReferenceIds.d.ts.map +1 -0
  36. package/dist/helpers/getReferenceIds.js +241 -0
  37. package/dist/helpers/getReferenceIds.js.map +1 -0
  38. package/dist/helpers/got.d.ts +60 -0
  39. package/dist/helpers/got.d.ts.map +1 -0
  40. package/dist/helpers/got.js +72 -0
  41. package/dist/helpers/got.js.map +1 -0
  42. package/dist/helpers/mapObjectProperties.d.ts +150 -0
  43. package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
  44. package/dist/helpers/mapObjectProperties.js +229 -0
  45. package/dist/helpers/mapObjectProperties.js.map +1 -0
  46. package/dist/helpers/normalizeAttributes.d.ts +213 -0
  47. package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
  48. package/dist/helpers/normalizeAttributes.js +243 -0
  49. package/dist/helpers/normalizeAttributes.js.map +1 -0
  50. package/dist/helpers/normalizeProperties.d.ts +168 -0
  51. package/dist/helpers/normalizeProperties.d.ts.map +1 -0
  52. package/dist/helpers/normalizeProperties.js +223 -0
  53. package/dist/helpers/normalizeProperties.js.map +1 -0
  54. package/dist/helpers/normalizeRequired.d.ts +159 -0
  55. package/dist/helpers/normalizeRequired.d.ts.map +1 -0
  56. package/dist/helpers/normalizeRequired.js +206 -0
  57. package/dist/helpers/normalizeRequired.js.map +1 -0
  58. package/dist/helpers/normalizeType.d.ts +81 -0
  59. package/dist/helpers/normalizeType.d.ts.map +1 -0
  60. package/dist/helpers/normalizeType.js +210 -0
  61. package/dist/helpers/normalizeType.js.map +1 -0
  62. package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
  63. package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
  64. package/dist/helpers/nullifyEmptyValues.js +191 -0
  65. package/dist/helpers/nullifyEmptyValues.js.map +1 -0
  66. package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
  67. package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
  68. package/dist/helpers/removeRequiredAndDefault.js +138 -0
  69. package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
  70. package/dist/helpers/validateId.d.ts +39 -0
  71. package/dist/helpers/validateId.d.ts.map +1 -0
  72. package/dist/helpers/validateId.js +51 -0
  73. package/dist/helpers/validateId.js.map +1 -0
  74. package/dist/index.d.ts +9 -0
  75. package/dist/index.d.ts.map +1 -0
  76. package/dist/index.js +21 -0
  77. package/dist/index.js.map +1 -0
  78. package/dist/ld/documentLoader.d.ts +8 -0
  79. package/dist/ld/documentLoader.d.ts.map +1 -0
  80. package/dist/ld/documentLoader.js +24 -0
  81. package/dist/ld/documentLoader.js.map +1 -0
  82. package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
  83. package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
  84. package/dist/ld/getLinkedDataAttributeType.js +32 -0
  85. package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
  86. package/dist/ld/getLinkedDataContext.d.ts +19 -0
  87. package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
  88. package/dist/ld/getLinkedDataContext.js +50 -0
  89. package/dist/ld/getLinkedDataContext.js.map +1 -0
  90. package/eslint.config.mjs +32 -52
  91. package/examples/credentials/createAccountCredential.ts +27 -0
  92. package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
  93. package/examples/index.ts +7 -0
  94. package/examples/schemas/FavoriteItemSchema.ts +27 -0
  95. package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
  96. package/examples/schemas/PreferencesSchema.ts +29 -0
  97. package/examples/schemas/ProfileSchema.ts +91 -0
  98. package/examples/schemas/Status.yaml +3 -0
  99. package/examples/schemas/StatusSchema.ts +12 -0
  100. package/jest.config.mjs +5 -0
  101. package/package.json +27 -20
  102. package/src/CredentialFactory.ts +392 -0
  103. package/src/Schema.ts +583 -0
  104. package/src/ValidationError.ts +90 -0
  105. package/src/Validator.ts +603 -0
  106. package/src/__tests__/CredentialFactory.test.ts +588 -0
  107. package/src/__tests__/Schema.test.ts +371 -0
  108. package/src/__tests__/ValidationError.test.ts +235 -0
  109. package/src/__tests__/Validator.test.ts +787 -0
  110. package/src/helpers/JsonSchema.ts +119 -0
  111. package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
  112. package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
  113. package/src/helpers/__tests__/createSchemasMap.test.ts +238 -0
  114. package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
  115. package/src/helpers/__tests__/got.test.ts +193 -0
  116. package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
  117. package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
  118. package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
  119. package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
  120. package/src/helpers/__tests__/normalizeType.test.ts +772 -0
  121. package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
  122. package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
  123. package/src/helpers/__tests__/validateId.test.ts +118 -0
  124. package/src/helpers/cleanupAttributes.ts +151 -0
  125. package/src/helpers/cleanupNulls.ts +106 -0
  126. package/src/helpers/createSchemasMap.ts +212 -0
  127. package/src/helpers/getReferenceIds.ts +273 -0
  128. package/src/helpers/got.ts +73 -0
  129. package/src/helpers/mapObjectProperties.ts +272 -0
  130. package/src/helpers/normalizeAttributes.ts +247 -0
  131. package/src/helpers/normalizeProperties.ts +249 -0
  132. package/src/helpers/normalizeRequired.ts +233 -0
  133. package/src/helpers/normalizeType.ts +235 -0
  134. package/src/helpers/nullifyEmptyValues.ts +207 -0
  135. package/src/helpers/removeRequiredAndDefault.ts +151 -0
  136. package/src/helpers/validateId.ts +53 -0
  137. package/src/index.ts +17 -0
  138. package/src/ld/__tests__/documentLoader.test.ts +57 -0
  139. package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
  140. package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
  141. package/src/ld/documentLoader.ts +28 -0
  142. package/src/ld/getLinkedDataAttributeType.ts +46 -0
  143. package/src/ld/getLinkedDataContext.ts +80 -0
  144. package/tsconfig.json +27 -0
  145. package/types/credentials-context.d.ts +14 -0
  146. package/types/security-context.d.ts +6 -0
  147. package/examples/Status.yaml +0 -3
  148. package/examples/createAccountCredential.js +0 -27
  149. package/examples/createMineSweeperScoreCredential.js +0 -63
  150. package/examples/index.js +0 -9
  151. package/src/CredentialFactory.js +0 -67
  152. package/src/CredentialFactory.spec.js +0 -131
  153. package/src/Schema.js +0 -104
  154. package/src/Schema.spec.js +0 -172
  155. package/src/ValidationError.js +0 -31
  156. package/src/Validator.js +0 -128
  157. package/src/Validator.spec.js +0 -355
  158. package/src/helpers/cleanupAttributes.js +0 -71
  159. package/src/helpers/cleanupNulls.js +0 -42
  160. package/src/helpers/getReferenceIds.js +0 -71
  161. package/src/helpers/mapObject.js +0 -65
  162. package/src/helpers/normalizeAttributes.js +0 -28
  163. package/src/helpers/normalizeProperties.js +0 -61
  164. package/src/helpers/normalizeRequired.js +0 -37
  165. package/src/helpers/normalizeType.js +0 -41
  166. package/src/helpers/nullifyEmptyValues.js +0 -57
  167. package/src/helpers/removeRequiredAndDefault.js +0 -30
  168. package/src/helpers/validateId.js +0 -19
  169. package/src/index.d.ts +0 -25
  170. package/src/index.js +0 -8
  171. package/src/ld/documentLoader.js +0 -25
  172. package/src/ld/documentLoader.spec.js +0 -12
  173. package/src/ld/getLinkedDataContext.js +0 -63
  174. package/src/ld/getLinkedDataType.js +0 -38
  175. /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
  176. /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
@@ -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
@@ -1,38 +0,0 @@
1
- 'use strict'
2
-
3
- // TODO: Add support for all types and formats, extend schema library with
4
- // support for additional formats, e.g. URL, Duration etc.
5
- const getLinkedDataType = schema => {
6
- const isOverriden = !!schema['@type']
7
-
8
- if (isOverriden) {
9
- return schema['@type']
10
- }
11
-
12
- const { type, format } = schema
13
-
14
- const isDate = format === 'date'
15
- const isNumber = type === 'number'
16
- const isInteger = type === 'integer'
17
- const isDateTime = format === 'date-time'
18
-
19
- if (isInteger) {
20
- return 'schema:Integer'
21
- }
22
-
23
- if (isNumber) {
24
- return 'schema:Number'
25
- }
26
-
27
- if (isDate) {
28
- return 'schema:Date'
29
- }
30
-
31
- if (isDateTime) {
32
- return 'schema:DateTime'
33
- }
34
-
35
- return
36
- }
37
-
38
- module.exports = getLinkedDataType
File without changes