-
Notifications
You must be signed in to change notification settings - Fork 146
/
annotation.ts
145 lines (130 loc) Β· 3.17 KB
/
annotation.ts
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
import _ from 'lodash'
import { PayloadValidationError } from './errors'
type AnnotationTarget = {
type: string
accept?: Record<string, any>[]
}
type AssignmentTarget = {
type: string
data: Record<string, any>
}
export type AnnotationAssignment = {
annotationId: string
target: AssignmentTarget
}
export type AnnotationData = {
id: string
targets: AnnotationTarget[]
}
export class Annotation {
private annotationData: AnnotationData
constructor(annotationData: AnnotationData) {
this.annotationData = annotationData
}
static isValidId(id: string): id is AnnotationId {
return Object.keys(availableAnnotations).includes(id)
}
/**
* Verifies if given assignment complies with the annotation definition
*/
validateAssignment(assignment: AnnotationAssignment): PayloadValidationError | undefined {
const { targets } = this.annotationData
const isValidTarget = targets.some((target) => {
if (target.type !== assignment.target.type) {
return false
}
return (
!target.accept ||
target.accept.some((requirements) => _.isMatch(assignment.target.data, requirements))
)
})
if (!isValidTarget) {
return {
type: 'InvalidPayload',
message: `Invalid annotation assignment for annotation "${this.annotationData.id}". Annotation target mismatch`
}
}
}
}
export const availableAnnotations = {
'Contentful:AggregateRoot': new Annotation({
id: 'Contentful:AggregateRoot',
targets: [
{
type: 'ContentType'
}
]
}),
'Contentful:ManagedByEnvironmentTemplate': new Annotation({
id: 'Contentful:ManagedByEnvironmentTemplate',
targets: [
{
type: 'ContentType'
}
]
}),
'Contentful:AggregateComponent': new Annotation({
id: 'Contentful:AggregateComponent',
targets: [
{
type: 'ContentTypeField',
accept: [
{
type: 'Link',
linkType: 'Entry'
},
{
type: 'Array',
items: {
type: 'Link',
linkType: 'Entry'
}
}
]
}
]
}),
'Contentful:GraphQLFieldResolver': new Annotation({
id: 'Contentful:GraphQLFieldResolver',
targets: [
{
type: 'ContentTypeField',
accept: [
{
type: 'Symbol'
},
{
type: 'Array',
items: {
type: 'Symbol'
}
},
{
type: 'Object'
}
]
}
]
})
}
export type AnnotationId = keyof typeof availableAnnotations
export function validateAnnotationAssignment(
assignment: AnnotationAssignment
): PayloadValidationError | undefined {
const annotation =
availableAnnotations[assignment.annotationId as keyof typeof availableAnnotations]
if (!annotation) {
return {
type: 'InvalidPayload',
message: `Invalid annotation assignment. Annotation "${assignment.annotationId}" not found`
}
}
return annotation.validateAssignment(assignment)
}
export type AnnotationLink = {
sys: {
type: 'Link'
linkType: 'Annotation'
id: AnnotationId
}
}