@lionweb/validation 0.7.0-beta.2 → 0.7.0-beta.21
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.
- package/CHANGELOG.md +6 -1
- package/dist/issues/ValidationIssue.d.ts +3 -3
- package/dist/issues/ValidationIssue.d.ts.map +1 -1
- package/dist/issues/ValidationIssue.js +1 -1
- package/dist/issues/ValidationIssue.js.map +1 -1
- package/dist/languages/LanguageUtils.d.ts.map +1 -1
- package/dist/languages/LionWebLanguageWrapper.d.ts.map +1 -1
- package/dist/runners/Utils.js +2 -2
- package/dist/runners/Utils.js.map +1 -1
- package/dist/validators/LionWebChunkDefinitions.d.ts +4 -4
- package/dist/validators/LionWebChunkDefinitions.d.ts.map +1 -1
- package/dist/validators/LionWebChunkDefinitions.js +80 -90
- package/dist/validators/LionWebChunkDefinitions.js.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.d.ts +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.d.ts.map +1 -1
- package/dist/validators/LionWebLanguageReferenceValidator.js +3 -3
- package/dist/validators/LionWebLanguageReferenceValidator.js.map +1 -1
- package/dist/validators/LionWebLanguageValidator.d.ts.map +1 -1
- package/dist/validators/LionWebLanguageValidator.js +2 -1
- package/dist/validators/LionWebLanguageValidator.js.map +1 -1
- package/dist/validators/LionWebSyntaxValidator.js +2 -2
- package/dist/validators/LionWebValidator.js +1 -1
- package/dist/validators/LionWebValidator.js.map +1 -1
- package/dist/validators/ValidationFunctions.d.ts +1 -1
- package/dist/validators/ValidationFunctions.d.ts.map +1 -1
- package/dist/validators/ValidationFunctions.js +7 -13
- package/dist/validators/ValidationFunctions.js.map +1 -1
- package/dist/validators/generic/SyntaxValidator.d.ts +7 -7
- package/dist/validators/generic/SyntaxValidator.d.ts.map +1 -1
- package/dist/validators/generic/SyntaxValidator.js +28 -33
- package/dist/validators/generic/SyntaxValidator.js.map +1 -1
- package/dist/validators/generic/index.d.ts +1 -1
- package/dist/validators/generic/index.d.ts.map +1 -1
- package/dist/validators/generic/index.js +1 -1
- package/dist/validators/generic/index.js.map +1 -1
- package/dist/validators/generic/schema/DefinitionSchema.d.ts +23 -0
- package/dist/validators/generic/schema/DefinitionSchema.d.ts.map +1 -0
- package/dist/validators/generic/schema/DefinitionSchema.js +43 -0
- package/dist/validators/generic/schema/DefinitionSchema.js.map +1 -0
- package/dist/validators/generic/{ValidationTypes.d.ts → schema/ValidationTypes.d.ts} +45 -12
- package/dist/validators/generic/schema/ValidationTypes.d.ts.map +1 -0
- package/dist/validators/generic/{ValidationTypes.js → schema/ValidationTypes.js} +18 -21
- package/dist/validators/generic/schema/ValidationTypes.js.map +1 -0
- package/dist/validators/generic/schema/index.d.ts +3 -0
- package/dist/validators/generic/schema/index.d.ts.map +1 -0
- package/dist/validators/generic/schema/index.js +3 -0
- package/dist/validators/generic/schema/index.js.map +1 -0
- package/package.json +37 -37
- package/src/issues/ValidationIssue.ts +4 -4
- package/src/runners/Utils.ts +2 -2
- package/src/validators/LionWebChunkDefinitions.ts +80 -90
- package/src/validators/LionWebLanguageReferenceValidator.ts +3 -3
- package/src/validators/LionWebLanguageValidator.ts +2 -1
- package/src/validators/LionWebSyntaxValidator.ts +2 -2
- package/src/validators/LionWebValidator.ts +1 -1
- package/src/validators/ValidationFunctions.ts +9 -15
- package/src/validators/generic/SyntaxValidator.ts +81 -87
- package/src/validators/generic/index.ts +1 -1
- package/src/validators/generic/schema/DefinitionSchema.ts +52 -0
- package/src/validators/generic/{ValidationTypes.ts → schema/ValidationTypes.ts} +65 -35
- package/src/validators/generic/schema/index.ts +2 -0
- package/dist/validators/generic/ValidationTypes.d.ts.map +0 -1
- package/dist/validators/generic/ValidationTypes.js.map +0 -1
- package/tsconfig.json +0 -8
|
@@ -9,40 +9,38 @@ import {
|
|
|
9
9
|
import { ValidationResult } from "./ValidationResult.js"
|
|
10
10
|
import {
|
|
11
11
|
isObjectDefinition,
|
|
12
|
-
isPrimitiveDefinition,
|
|
13
|
-
ObjectDefinition,
|
|
14
|
-
PrimitiveDefinition,
|
|
15
|
-
TypeDefinition,
|
|
12
|
+
isPrimitiveDefinition, ObjectDefinition, PrimitiveDefinition, DefinitionSchema,
|
|
16
13
|
UnknownObjectType
|
|
17
|
-
} from "./
|
|
14
|
+
} from "./schema/index.js"
|
|
18
15
|
|
|
19
16
|
/**
|
|
20
|
-
* Syntax Validator
|
|
21
|
-
* definitions given in `
|
|
17
|
+
* Syntax Validator checks whether objects are structurally conforming to the
|
|
18
|
+
* definitions given in `schema`.
|
|
22
19
|
*/
|
|
23
20
|
export class SyntaxValidator {
|
|
24
21
|
validationResult: ValidationResult
|
|
25
|
-
|
|
22
|
+
schema: DefinitionSchema
|
|
26
23
|
|
|
27
|
-
constructor(validationResult: ValidationResult,
|
|
24
|
+
constructor(validationResult: ValidationResult, schema: DefinitionSchema) {
|
|
28
25
|
this.validationResult = validationResult
|
|
29
|
-
this.
|
|
26
|
+
this.schema = schema
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
/**
|
|
33
30
|
* Check whether `obj` is a JSON object that conforms to the definition of `expectedType`.
|
|
34
|
-
* All errors found will be
|
|
31
|
+
* All errors found will be added to the `validationResult` object.
|
|
35
32
|
* @param obj The object to validate.
|
|
36
33
|
* @param expectedType The expected type of the object.
|
|
37
34
|
*/
|
|
38
35
|
validate(obj: unknown, expectedType: string) {
|
|
39
36
|
const object = obj as UnknownObjectType
|
|
40
|
-
const typeDef = this.
|
|
37
|
+
const typeDef = this.schema.getDefinition(expectedType)
|
|
38
|
+
|
|
41
39
|
if (typeDef === undefined) {
|
|
42
40
|
throw new Error(`SyntaxValidator.validate: cannot find definition for ${expectedType}`)
|
|
43
|
-
} else if (isObjectDefinition(typeDef)){
|
|
41
|
+
} else if (isObjectDefinition(typeDef)) {
|
|
44
42
|
this.validateObjectProperties(expectedType, typeDef, object, new JsonContext(null, ["$"]))
|
|
45
|
-
} else if(
|
|
43
|
+
} else if (isPrimitiveDefinition(typeDef)) {
|
|
46
44
|
this.validatePrimitiveValue("$", typeDef, object, new JsonContext(null, ["$"]))
|
|
47
45
|
}
|
|
48
46
|
}
|
|
@@ -50,7 +48,7 @@ export class SyntaxValidator {
|
|
|
50
48
|
/**
|
|
51
49
|
* Validate whether `object` is structured conform the properties in `propertyDef`
|
|
52
50
|
* @param originalProperty The property of which `object` it the value
|
|
53
|
-
* @param typeDef
|
|
51
|
+
* @param typeDef The property definitions that are being validated
|
|
54
52
|
* @param object The object being validated
|
|
55
53
|
* @param jsonContext The location in the JSON
|
|
56
54
|
* @private
|
|
@@ -59,93 +57,89 @@ export class SyntaxValidator {
|
|
|
59
57
|
if (typeDef === null || typeDef === undefined) {
|
|
60
58
|
return
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
continue
|
|
75
|
-
}
|
|
76
|
-
if (!propertyDef.mayBeNull && propertyValue === null) {
|
|
77
|
-
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propertyDef.property))
|
|
78
|
-
continue
|
|
60
|
+
if (typeof object !== "object") {
|
|
61
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, originalProperty, "object", typeof object))
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
for (const propertyDef of typeDef.properties) {
|
|
65
|
+
const expectedTypeDef = this.schema.getDefinition(propertyDef.type)
|
|
66
|
+
const validator = propertyDef.validate!
|
|
67
|
+
const propertyValue = object[propertyDef.name]
|
|
68
|
+
if (propertyValue === undefined) {
|
|
69
|
+
if (!propertyDef.isOptional) {
|
|
70
|
+
this.validationResult.issue(new Syntax_PropertyMissingIssue(jsonContext, propertyDef.name))
|
|
79
71
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
if (!propertyDef.mayBeNull && propertyValue === null) {
|
|
75
|
+
this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propertyDef.name))
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
if (propertyDef.mayBeNull && propertyValue === null) {
|
|
79
|
+
// Ok, stop checking, continue with next property def
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
if (propertyDef.isList) {
|
|
83
|
+
// Check whether value is an array
|
|
84
|
+
if (!Array.isArray(propertyValue)) {
|
|
85
|
+
const newContext = jsonContext.concat(propertyDef.name)
|
|
86
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.name, "array", typeof propertyValue))
|
|
87
|
+
return
|
|
83
88
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this.validationResult.issue(new
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const newContext = jsonContext.concat(propertyDef.property, index)
|
|
94
|
-
if (item === null) {
|
|
95
|
-
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, propertyDef.property, index))
|
|
96
|
-
} else {
|
|
97
|
-
if (expectedTypeDef !== undefined) {
|
|
98
|
-
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
99
|
-
// propertyValue should be a primitive as it has no property definitions
|
|
100
|
-
if (this.validatePrimitiveValue(propertyDef.property, expectedTypeDef, item, jsonContext)) {
|
|
101
|
-
validator.apply(null, [item, this.validationResult, newContext, propertyDef])
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
// propertyValue should be an object, validate its properties
|
|
105
|
-
this.validateObjectProperties(propertyDef.property, expectedTypeDef, item as UnknownObjectType, newContext)
|
|
89
|
+
// If an array, validate every item in the array
|
|
90
|
+
(propertyValue as UnknownObjectType[]).forEach((item, index) => {
|
|
91
|
+
const newContext = jsonContext.concat(propertyDef.name, index)
|
|
92
|
+
if (item === null) {
|
|
93
|
+
this.validationResult.issue(new Syntax_ArrayContainsNull_Issue(newContext, propertyDef.name, index))
|
|
94
|
+
} else {
|
|
95
|
+
if (expectedTypeDef !== undefined) {
|
|
96
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
97
|
+
if (this.validatePrimitiveValue(propertyDef.name, expectedTypeDef, item, jsonContext)) {
|
|
106
98
|
validator.apply(null, [item, this.validationResult, newContext, propertyDef])
|
|
107
99
|
}
|
|
108
100
|
} else {
|
|
109
|
-
|
|
101
|
+
// propertyValue should be an object, validate its properties
|
|
102
|
+
this.validateObjectProperties(propertyDef.name, expectedTypeDef, item as UnknownObjectType, newContext)
|
|
103
|
+
validator.apply(null, [item, this.validationResult, newContext, propertyDef])
|
|
110
104
|
}
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error(`Expected type '${propertyDef.type} has neither property defs, nor a validator.`)
|
|
111
107
|
}
|
|
112
|
-
})
|
|
113
|
-
} else {
|
|
114
|
-
const newContext = jsonContext.concat(propertyDef.property)
|
|
115
|
-
if (Array.isArray(propertyValue)) {
|
|
116
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.property, propertyDef.expectedType, "array"))
|
|
117
|
-
return
|
|
118
108
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
109
|
+
})
|
|
110
|
+
} else {
|
|
111
|
+
const newContext = jsonContext.concat(propertyDef.name)
|
|
112
|
+
if (Array.isArray(propertyValue)) {
|
|
113
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(newContext, propertyDef.name, propertyDef.type, "array"))
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
// Single valued property, validate it
|
|
117
|
+
if (expectedTypeDef !== undefined) {
|
|
118
|
+
if (isPrimitiveDefinition(expectedTypeDef)) {
|
|
119
|
+
// propertyValue should be a primitive as it has no property definitions
|
|
120
|
+
if (this.validatePrimitiveValue(propertyDef.name, expectedTypeDef, propertyValue, jsonContext)) {
|
|
129
121
|
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
|
|
130
|
-
} else {
|
|
131
|
-
throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else")
|
|
132
122
|
}
|
|
123
|
+
} else if (isObjectDefinition(expectedTypeDef)) {
|
|
124
|
+
// propertyValue should be an object, validate its properties
|
|
125
|
+
this.validateObjectProperties(propertyDef.name, expectedTypeDef, propertyValue as UnknownObjectType, newContext)
|
|
126
|
+
validator.apply(null, [propertyValue, this.validationResult, newContext, propertyDef])
|
|
133
127
|
} else {
|
|
134
|
-
throw new Error(
|
|
128
|
+
throw new Error("EXPECTING ObjectDefinition or PrimitiveDefinition, but got something else")
|
|
135
129
|
}
|
|
130
|
+
} else {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Expected single type '${propertyDef.type}' for '${propertyDef.name}' at ${newContext.toString()} has neither property defs, nor a validator.`
|
|
133
|
+
)
|
|
136
134
|
}
|
|
137
135
|
}
|
|
138
|
-
|
|
136
|
+
}
|
|
137
|
+
this.checkStrayProperties(object, typeDef, jsonContext)
|
|
139
138
|
}
|
|
140
|
-
|
|
141
|
-
validatePrimitiveValue(propertyName: string, propDef: PrimitiveDefinition, object: unknown, jsonContext: JsonContext): boolean {
|
|
142
|
-
// if (!propDef.mayBeNull && (object === null || object === undefined)) {
|
|
143
|
-
// this.validationResult.issue(new Syntax_PropertyNullIssue(jsonContext, propDef.property))
|
|
144
|
-
// return false
|
|
145
|
-
// }
|
|
146
139
|
|
|
140
|
+
validatePrimitiveValue(propertyName: string, propDef: PrimitiveDefinition, object: unknown, jsonContext: JsonContext): boolean {
|
|
147
141
|
if (typeof object !== propDef.primitiveType) {
|
|
148
|
-
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType,typeof object))
|
|
142
|
+
this.validationResult.issue(new Syntax_PropertyTypeIssue(jsonContext, propertyName, propDef.primitiveType, typeof object))
|
|
149
143
|
return false
|
|
150
144
|
}
|
|
151
145
|
propDef.validate!(object, this.validationResult, jsonContext)
|
|
@@ -160,8 +154,8 @@ export class SyntaxValidator {
|
|
|
160
154
|
*/
|
|
161
155
|
checkStrayProperties(obj: UnknownObjectType, def: ObjectDefinition, context: JsonContext) {
|
|
162
156
|
const own = Object.getOwnPropertyNames(obj)
|
|
163
|
-
const defined = def.map(pdef => pdef.
|
|
164
|
-
own.forEach(
|
|
157
|
+
const defined = def.properties.map(pdef => pdef.name)
|
|
158
|
+
own.forEach(ownProp => {
|
|
165
159
|
if (!defined.includes(ownProp)) {
|
|
166
160
|
this.validationResult.issue(new Syntax_PropertyUnknownIssue(context, ownProp))
|
|
167
161
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Definition, PrimitiveDefinition, TaggedUnionDefinition } from "./ValidationTypes.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A collection of object and primitive definitions describing JSON objects.
|
|
5
|
+
* Used to
|
|
6
|
+
* - validate an incoming JSON object
|
|
7
|
+
* - generate the corresponding TypeScript type definitions
|
|
8
|
+
* - generate handlers for the JSOn objects (in @lionweb/server)
|
|
9
|
+
*/
|
|
10
|
+
export class DefinitionSchema {
|
|
11
|
+
unionDefinition: TaggedUnionDefinition | undefined
|
|
12
|
+
/**
|
|
13
|
+
* Mapping from extenden object type name to list of extending Object Definitions
|
|
14
|
+
*/
|
|
15
|
+
definitionsMap: Map<string, Definition> = new Map<string, Definition>()
|
|
16
|
+
|
|
17
|
+
constructor(definitions: Definition[], taggedUnion?: TaggedUnionDefinition) {
|
|
18
|
+
this.add(definitions)
|
|
19
|
+
this.unionDefinition = taggedUnion
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getDefinition(name: string): Definition | undefined {
|
|
23
|
+
return this.definitionsMap.get(name)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
add(definitions :Definition[] | Definition) {
|
|
27
|
+
if (!Array.isArray(definitions)) {
|
|
28
|
+
definitions = [definitions]
|
|
29
|
+
}
|
|
30
|
+
for(const def of definitions) {
|
|
31
|
+
this.definitionsMap.set(def.name, def)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
isTagProperty(propertyName: string): boolean {
|
|
36
|
+
return this.unionDefinition?.unionProperty === propertyName
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
definitions(): Definition[] {
|
|
40
|
+
return Array.from(this.definitionsMap.values())
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
isUnionDiscriminator(propDef: PrimitiveDefinition): boolean {
|
|
44
|
+
return this.unionDefinition?.unionDiscriminator === propDef.name
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
joinDefinitions(...schema: DefinitionSchema[]): void {
|
|
48
|
+
schema.forEach(sch => {
|
|
49
|
+
this.add(sch.definitions())
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonContext } from "@lionweb/json-utils"
|
|
2
|
-
import { ValidationResult } from "
|
|
2
|
+
import { ValidationResult } from "../ValidationResult.js"
|
|
3
3
|
|
|
4
4
|
export type UnknownObjectType = { [key: string]: unknown }
|
|
5
5
|
|
|
@@ -15,11 +15,11 @@ export type PropertyDefinition = {
|
|
|
15
15
|
/**
|
|
16
16
|
* The property name
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
name: string
|
|
19
19
|
/**
|
|
20
20
|
* The expected type of the property value
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
type: string
|
|
23
23
|
/**
|
|
24
24
|
* Whether the property value is allowed to be null
|
|
25
25
|
*/
|
|
@@ -39,38 +39,17 @@ export type PropertyDefinition = {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
export type ValidatorFunction = <T>(obj: T, result: ValidationResult, ctx: JsonContext, pdef?: PropertyDefinition) => void
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Default for the `validation` property, does nothing.
|
|
45
|
-
* @param object
|
|
46
|
-
* @param result
|
|
47
|
-
* @param ctx
|
|
48
|
-
* @param pdef
|
|
49
|
-
*/
|
|
50
42
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
-
function emptyValidation<T>(object: T, result: ValidationResult, ctx: JsonContext, pdef?: PropertyDefinition): void {}
|
|
43
|
+
export function emptyValidation<T>(object: T, result: ValidationResult, ctx: JsonContext, pdef?: PropertyDefinition): void {}
|
|
52
44
|
|
|
53
45
|
// Make boolean argument more readable.
|
|
54
46
|
export const MAY_BE_NULL = true
|
|
55
47
|
|
|
56
48
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @param propDef
|
|
59
|
-
* @constructor
|
|
49
|
+
* Definition of a primitive type.
|
|
60
50
|
*/
|
|
61
|
-
export function PropertyDef(propDef: PropertyDefinition): PropertyDefinition {
|
|
62
|
-
const { property, expectedType, mayBeNull = false, isList = false, isOptional = false, validate = emptyValidation } = propDef
|
|
63
|
-
return {
|
|
64
|
-
property: property,
|
|
65
|
-
expectedType: expectedType,
|
|
66
|
-
isList: isList,
|
|
67
|
-
mayBeNull: mayBeNull,
|
|
68
|
-
isOptional: isOptional,
|
|
69
|
-
validate: validate
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
51
|
export type PrimitiveDefinition = {
|
|
52
|
+
name: string,
|
|
74
53
|
/**
|
|
75
54
|
* The expected type of the property value
|
|
76
55
|
*/
|
|
@@ -80,25 +59,76 @@ export type PrimitiveDefinition = {
|
|
|
80
59
|
*/
|
|
81
60
|
validate?: ValidatorFunction
|
|
82
61
|
}
|
|
62
|
+
|
|
83
63
|
/**
|
|
84
|
-
*
|
|
64
|
+
* Definition of an object type.
|
|
65
|
+
*/
|
|
66
|
+
export type ObjectDefinition = {
|
|
67
|
+
name: string,
|
|
68
|
+
properties: PropertyDefinition[],
|
|
69
|
+
/**
|
|
70
|
+
* The name of the tagged union that this type belongs to
|
|
71
|
+
*/
|
|
72
|
+
taggedUnionType?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type Definition = ObjectDefinition | PrimitiveDefinition
|
|
76
|
+
/**
|
|
77
|
+
* Defionition of tagged union.
|
|
78
|
+
*/
|
|
79
|
+
export type TaggedUnionDefinition = {
|
|
80
|
+
/**
|
|
81
|
+
* The tagged union "super" type
|
|
82
|
+
*/
|
|
83
|
+
unionType: string,
|
|
84
|
+
/**
|
|
85
|
+
* The primitive property type that is the discriminator or tag
|
|
86
|
+
*/
|
|
87
|
+
unionDiscriminator: string,
|
|
88
|
+
/**
|
|
89
|
+
* The name of the property in an object that contains the discriminator value
|
|
90
|
+
*/
|
|
91
|
+
unionProperty: string
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Easy way to create a PrimitiveDefinition typed object with default values.
|
|
85
95
|
* @param propDef
|
|
86
96
|
* @constructor
|
|
87
97
|
*/
|
|
88
98
|
export function PrimitiveDef(propDef: PrimitiveDefinition): PrimitiveDefinition {
|
|
89
|
-
const { primitiveType, validate = emptyValidation } = propDef
|
|
99
|
+
const { name, primitiveType, validate = emptyValidation } = propDef
|
|
90
100
|
return {
|
|
101
|
+
name: name,
|
|
91
102
|
primitiveType: primitiveType,
|
|
92
103
|
validate: validate
|
|
93
104
|
}
|
|
94
105
|
}
|
|
95
|
-
export type ObjectDefinition = PropertyDefinition[]
|
|
96
|
-
export type TypeDefinition = ObjectDefinition | PrimitiveDefinition
|
|
97
106
|
|
|
98
|
-
|
|
99
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Easy way to create a PropertyDefinition typed object with default values.
|
|
109
|
+
* @param propDef
|
|
110
|
+
* @constructor
|
|
111
|
+
*/
|
|
112
|
+
export function PropertyDef(propDef: PropertyDefinition): PropertyDefinition {
|
|
113
|
+
const { name, type, mayBeNull = false, isList = false, isOptional = false, validate = emptyValidation } = propDef
|
|
114
|
+
return {
|
|
115
|
+
name: name,
|
|
116
|
+
type: type,
|
|
117
|
+
isList: isList,
|
|
118
|
+
mayBeNull: mayBeNull,
|
|
119
|
+
isOptional: isOptional,
|
|
120
|
+
validate: validate
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function isObjectDefinition(def: Definition | undefined): def is ObjectDefinition {
|
|
125
|
+
return (def !== undefined) && Array.isArray((def as ObjectDefinition)?.properties)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function isPrimitiveDefinition(def: Definition | undefined): def is PrimitiveDefinition {
|
|
129
|
+
return (def !== undefined) && (def as PrimitiveDefinition)?.primitiveType !== undefined
|
|
100
130
|
}
|
|
101
131
|
|
|
102
|
-
export function
|
|
103
|
-
return
|
|
132
|
+
export function isJavaScriptPrimitive(type: string): boolean {
|
|
133
|
+
return ["number", "string", "boolean"].includes(type)
|
|
104
134
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationTypes.d.ts","sourceRoot":"","sources":["../../../src/validators/generic/ValidationTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAExD,MAAM,MAAM,iBAAiB,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AAE1D;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC7B;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAA;CAC/B,C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