@lionweb/validation 0.7.0-beta.8 → 0.7.0-beta.9
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/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/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 +4 -4
- package/dist/validators/ValidationFunctions.js.map +1 -1
- package/dist/validators/generic/SyntaxValidator.d.ts +21 -7
- package/dist/validators/generic/SyntaxValidator.d.ts.map +1 -1
- package/dist/validators/generic/SyntaxValidator.js +78 -34
- 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 +26 -0
- package/dist/validators/generic/schema/DefinitionSchema.d.ts.map +1 -0
- package/dist/validators/generic/schema/DefinitionSchema.js +75 -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 +4 -4
- package/src/runners/Utils.ts +2 -2
- package/src/validators/LionWebChunkDefinitions.ts +80 -90
- package/src/validators/LionWebSyntaxValidator.ts +2 -2
- package/src/validators/LionWebValidator.ts +1 -1
- package/src/validators/ValidationFunctions.ts +5 -5
- package/src/validators/generic/SyntaxValidator.ts +134 -87
- package/src/validators/generic/index.ts +1 -1
- package/src/validators/generic/schema/DefinitionSchema.ts +84 -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
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Definition, isObjectDefinition, 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
|
+
*/
|
|
9
|
+
export class DefinitionSchema {
|
|
10
|
+
unionDefinitions: TaggedUnionDefinition[] = []
|
|
11
|
+
/**
|
|
12
|
+
* Mapping from extenden object type name to list of extending Object Definitions
|
|
13
|
+
*/
|
|
14
|
+
taggedUnions: Map<string, Definition[]> = new Map<string, Definition[]>()
|
|
15
|
+
definitionsMap: Map<string, Definition> = new Map<string, Definition>()
|
|
16
|
+
|
|
17
|
+
constructor(taggedUnions: TaggedUnionDefinition[], definitions: Definition[]) {
|
|
18
|
+
this.add(definitions)
|
|
19
|
+
this.addTaggedUnion(taggedUnions)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getDefinition(name: string): Definition | undefined {
|
|
23
|
+
return this.definitionsMap.get(name)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
addTaggedUnion(defs: TaggedUnionDefinition[]){
|
|
27
|
+
this.unionDefinitions.push(...defs)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
add(definitions :Definition[] | Definition) {
|
|
31
|
+
if (!Array.isArray(definitions)) {
|
|
32
|
+
definitions = [definitions]
|
|
33
|
+
}
|
|
34
|
+
for(const def of definitions) {
|
|
35
|
+
if (isObjectDefinition(def)) {
|
|
36
|
+
if (def.taggedUnionType !== "") {
|
|
37
|
+
let existingExtends = this.taggedUnions.get(def.taggedUnionType!)
|
|
38
|
+
if (existingExtends === undefined) {
|
|
39
|
+
existingExtends = []
|
|
40
|
+
this.taggedUnions.set(def.taggedUnionType!, existingExtends)
|
|
41
|
+
}
|
|
42
|
+
existingExtends.push(def)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.definitionsMap.set(def.name, def)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
isTagProperty(propertyName: string): boolean {
|
|
50
|
+
return this.unionDefinitions.find(def => def.unionProperty === propertyName) !== undefined
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
definitions(): Definition[] {
|
|
54
|
+
return Array.from(this.definitionsMap.values())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getTaggedUnionDefinition(name: string): TaggedUnionDefinition | undefined {
|
|
58
|
+
return this.unionDefinitions.find(def => def.unionType === name)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
isUnionDiscriminator(propDef: PrimitiveDefinition): boolean {
|
|
62
|
+
return this.unionDefinitions.find(def => def.unionDiscriminator === propDef.name) !== undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
extending(name: string): Definition[] {
|
|
66
|
+
const result = this.taggedUnions.get(name)
|
|
67
|
+
if (result === undefined) {
|
|
68
|
+
return []
|
|
69
|
+
} else {
|
|
70
|
+
return Array.from(result.values())
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static join(...schema: DefinitionSchema[]): DefinitionSchema {
|
|
75
|
+
const result = new DefinitionSchema([], [])
|
|
76
|
+
schema.forEach(sch => {
|
|
77
|
+
sch.definitions().forEach(value => {
|
|
78
|
+
result.add([value])
|
|
79
|
+
})
|
|
80
|
+
result.addTaggedUnion(sch.unionDefinitions)
|
|
81
|
+
})
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -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,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,gBAAgB,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,kBAAkB,KAAK,IAAI,CAAA;AAa1H,eAAO,MAAM,WAAW,OAAO,CAAA;AAE/B;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,kBAAkB,CAU3E;AAED,MAAM,MAAM,mBAAmB,GAAG;IAC9B;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH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