raml-typesystem
Advanced tools
Comparing version 0.0.58 to 0.0.59
@@ -79,3 +79,3 @@ "use strict"; | ||
if (_strict === void 0) { _strict = true; } | ||
if (_annotations === void 0) { _annotations = undefined; } | ||
if (_annotations === void 0) { _annotations = {}; } | ||
if (_isSingle === void 0) { _isSingle = false; } | ||
@@ -91,2 +91,3 @@ if (_empty === void 0) { _empty = false; } | ||
this._empty = _empty; | ||
this._scalarsAnnotations = {}; | ||
} | ||
@@ -164,2 +165,13 @@ Example.prototype.isEmpty = function () { | ||
}; | ||
Example.prototype.scalarsAnnotations = function () { | ||
return this._scalarsAnnotations; | ||
}; | ||
Example.prototype.registerScalarAnnotatoion = function (a, pName) { | ||
var aMap = this._scalarsAnnotations[pName]; | ||
if (!aMap) { | ||
aMap = {}; | ||
this._scalarsAnnotations[pName] = aMap; | ||
} | ||
aMap[a.facetName()] = a; | ||
}; | ||
return Example; | ||
@@ -178,17 +190,22 @@ }()); | ||
else { | ||
var displayName = exampleObj.displayName; | ||
var description = exampleObj.description; | ||
var strict = exampleObj.strict; | ||
var aObj = null; | ||
var annotationNames = Object.keys(exampleObj).filter(function (x) { return x.charAt(0) == "("; }); | ||
if (annotationNames.length > 0) { | ||
aObj = {}; | ||
for (var _i = 0, annotationNames_1 = annotationNames; _i < annotationNames_1.length; _i++) { | ||
var aName = annotationNames_1[_i]; | ||
var aVal = exampleObj[aName]; | ||
aName = aName.substring(1, aName.length - 1); | ||
aObj[aName] = aVal; | ||
} | ||
var displayName = scalarValue(exampleObj, "displayName"); | ||
var description = scalarValue(exampleObj, "description"); | ||
var strict = scalarValue(exampleObj, "strict"); | ||
var aObj = {}; | ||
scalarAnnotaitons(exampleObj).forEach(function (x) { | ||
aObj[x.facetName()] = x; | ||
}); | ||
example = new Example(val, name, displayName, description, strict, aObj, isSingle); | ||
for (var _i = 0, _a = scalarAnnotaitons(exampleObj["displayName"]); _i < _a.length; _i++) { | ||
var a = _a[_i]; | ||
example.registerScalarAnnotatoion(a, "displayName"); | ||
} | ||
example = new Example(val, name, displayName, description, strict, aObj, isSingle); | ||
for (var _b = 0, _c = scalarAnnotaitons(exampleObj["description"]); _b < _c.length; _b++) { | ||
var a = _c[_b]; | ||
example.registerScalarAnnotatoion(a, "description"); | ||
} | ||
for (var _d = 0, _e = scalarAnnotaitons(exampleObj["strict"]); _d < _e.length; _d++) { | ||
var a = _e[_d]; | ||
example.registerScalarAnnotatoion(a, "strict"); | ||
} | ||
} | ||
@@ -201,2 +218,23 @@ } | ||
}; | ||
function scalarValue(obj, propName) { | ||
var pVal = obj[propName]; | ||
if (pVal != null && typeof (pVal) == "object") { | ||
return pVal["value"]; | ||
} | ||
return pVal; | ||
} | ||
function scalarAnnotaitons(obj) { | ||
var result = []; | ||
if (!obj || typeof (obj) != "object") { | ||
return result; | ||
} | ||
for (var _i = 0, _a = Object.keys(obj).filter(function (x) { return x.length > 0 && x.charAt(0) == "(" && x.charAt(x.length - 1) == ")"; }); _i < _a.length; _i++) { | ||
var aKey = _a[_i]; | ||
var aName = aKey.substring(1, aKey.length - 1); | ||
var aVal = obj[aKey]; | ||
var a = new meta.Annotation(aName, aVal); | ||
result.push(a); | ||
} | ||
return result; | ||
} | ||
function exampleFromInheritedType(inheritedType) { | ||
@@ -203,0 +241,0 @@ var result = []; |
@@ -30,6 +30,9 @@ /// <reference path="../../typings/main.d.ts" /> | ||
} | ||
export declare class Annotation extends MetaInfo { | ||
export declare class Annotation extends MetaInfo implements tsInterfaces.IAnnotation { | ||
constructor(name: string, value: any); | ||
private _ownerFacet; | ||
validateSelf(registry: ts.TypeRegistry, ofExample?: boolean): ts.Status; | ||
kind(): tsInterfaces.MetaInformationKind; | ||
ownerFacet(): tsInterfaces.ITypeFacet; | ||
setOwnerFacet(ownerFacet: tsInterfaces.ITypeFacet): void; | ||
} | ||
@@ -66,3 +69,2 @@ export declare class FacetDeclaration extends MetaInfo { | ||
constructor(); | ||
validateSelf(registry: ts.TypeRegistry): ts.Status; | ||
kind(): tsInterfaces.MetaInformationKind; | ||
@@ -72,3 +74,2 @@ } | ||
constructor(value: any); | ||
validateSelf(registry: ts.TypeRegistry): ts.Status; | ||
kind(): tsInterfaces.MetaInformationKind; | ||
@@ -81,2 +82,3 @@ } | ||
validateSelf(registry: ts.TypeRegistry): ts.Status; | ||
private checkScalarProperty(exampleObj, exampleName, y, registry, status); | ||
kind(): tsInterfaces.MetaInformationKind; | ||
@@ -83,0 +85,0 @@ } |
@@ -126,3 +126,3 @@ "use strict"; | ||
} | ||
result.setValidationPath({ name: "(" + this.facetName() + ")" }); | ||
ts.setValidationPath(result, { name: "(" + this.facetName() + ")" }); | ||
return result; | ||
@@ -133,2 +133,8 @@ }; | ||
}; | ||
Annotation.prototype.ownerFacet = function () { | ||
return this._ownerFacet; | ||
}; | ||
Annotation.prototype.setOwnerFacet = function (ownerFacet) { | ||
this._ownerFacet = ownerFacet; | ||
}; | ||
return Annotation; | ||
@@ -214,2 +220,7 @@ }(MetaInfo)); | ||
} | ||
var exampleScalarProperties = [ | ||
{ propName: "strict", propType: "boolean", messageEntry: messageRegistry.STRICT_BOOLEAN }, | ||
{ propName: "displayName", propType: "string", messageEntry: messageRegistry.DISPLAY_NAME_STRING }, | ||
{ propName: "description", propType: "string", messageEntry: messageRegistry.DESCRIPTION_STRING } | ||
]; | ||
var Example = (function (_super) { | ||
@@ -224,3 +235,3 @@ __extends(Example, _super); | ||
var aStatus = this.validateAnnotations(registry); | ||
aStatus.setValidationPath({ name: this.facetName() }); | ||
ts.setValidationPath(aStatus, { name: this.facetName() }); | ||
status.addSubStatus(aStatus); | ||
@@ -232,11 +243,34 @@ return status; | ||
var isVal = false; | ||
var result = ts.ok(); | ||
if (typeof val === "object" && val) { | ||
if (val.value) { | ||
if (val.strict === false) { | ||
return ts.ok(); | ||
for (var _i = 0, exampleScalarProperties_1 = exampleScalarProperties; _i < exampleScalarProperties_1.length; _i++) { | ||
var y = exampleScalarProperties_1[_i]; | ||
var propName = y.propName; | ||
var propType = y.propType; | ||
var propObj = val[propName]; | ||
if (propObj && typeof propObj != propType) { | ||
if (typeof (propObj) == "object") { | ||
Object.keys(propObj).forEach(function (key) { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), propObj[key]); | ||
var aRes = a.validateSelf(registry, true); | ||
ts.setValidationPath(aRes, { | ||
name: "example", | ||
child: { name: propName, child: { name: key } } | ||
}); | ||
result.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
if (!propObj.value && typeof propObj.value != propType) { | ||
var s = ts.error(y.messageEntry, this); | ||
var vp = propObj.value ? { name: "value" } : null; | ||
ts.setValidationPath(s, { name: "example", child: { name: propName, child: vp } }); | ||
result.addSubStatus(s); | ||
} | ||
} | ||
} | ||
if (val.strict && typeof val.strict != "boolean") { | ||
var s = ts.error(messageRegistry.STRICT_BOOLEAN, this); | ||
s.setValidationPath({ name: "example", child: { name: "strict" } }); | ||
return s; | ||
if (val.strict === false || (typeof (val.strict) == "object" && val.strict.value === false)) { | ||
return result; | ||
} | ||
@@ -248,5 +282,6 @@ val = val.value; | ||
var rr = parseExampleIfNeeded(val, this.owner()); | ||
if (rr instanceof ts.Status) { | ||
rr.setValidationPath({ name: "example" }); | ||
return rr; | ||
if (rr instanceof ts.Status && !rr.isOk()) { | ||
ts.setValidationPath(rr, { name: "example" }); | ||
result.addSubStatus(rr); | ||
return result; | ||
} | ||
@@ -261,11 +296,11 @@ var valOwner = this.owner().validateDirect(rr, true, false); | ||
if (isVal) { | ||
x.setValidationPath({ name: "example", child: { name: "value" } }); | ||
ts.setValidationPath(x, { name: "example", child: { name: "value" } }); | ||
} | ||
else { | ||
x.setValidationPath({ name: "example" }); | ||
ts.setValidationPath(x, { name: "example" }); | ||
} | ||
}); | ||
return c; | ||
result.addSubStatus(c); | ||
} | ||
return ts.ok(); | ||
return result; | ||
}; | ||
@@ -320,6 +355,8 @@ Example.prototype.validateAnnotations = function (registry) { | ||
Required.prototype.validateSelf = function (registry) { | ||
var result = _super.prototype.validateSelf.call(this, registry); | ||
if (typeof this.value() !== "boolean") { | ||
return ts.error(messageRegistry.REQUIRED_BOOLEAN, this); | ||
result = ts.error(messageRegistry.REQUIRED_BOOLEAN, this); | ||
ts.setValidationPath(result, { name: this.facetName() }); | ||
} | ||
return ts.ok(); | ||
return result; | ||
}; | ||
@@ -337,5 +374,2 @@ Required.prototype.kind = function () { | ||
} | ||
HasPropertiesFacet.prototype.validateSelf = function (registry) { | ||
return ts.ok(); | ||
}; | ||
HasPropertiesFacet.prototype.kind = function () { | ||
@@ -352,5 +386,2 @@ return tsInterfaces.MetaInformationKind.HasPropertiesFacet; | ||
} | ||
AllowedTargets.prototype.validateSelf = function (registry) { | ||
return ts.ok(); | ||
}; | ||
AllowedTargets.prototype.kind = function () { | ||
@@ -405,17 +436,27 @@ return tsInterfaces.MetaInformationKind.AllowedTargets; | ||
Object.keys(v).forEach(function (x) { | ||
if (v[x]) { | ||
var val = v[x].value; | ||
var exampleObj = v[x]; | ||
if (exampleObj) { | ||
if (typeof exampleObj == "object" && exampleObj.value) { | ||
Object.keys(exampleObj).forEach(function (key) { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), v[x][key]); | ||
var aRes = a.validateSelf(registry, true); | ||
ts.setValidationPath(aRes, { name: "examples", child: { name: x, child: { name: key } } }); | ||
rs.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
var val = exampleObj.value; | ||
var noVal = !val; | ||
if (noVal) { | ||
val = v[x]; | ||
val = exampleObj; | ||
} | ||
else { | ||
if (v[x].strict === false) { | ||
for (var _i = 0, exampleScalarProperties_2 = exampleScalarProperties; _i < exampleScalarProperties_2.length; _i++) { | ||
var y = exampleScalarProperties_2[_i]; | ||
_this.checkScalarProperty(exampleObj, x, y, registry, rs); | ||
} | ||
if (exampleObj.strict === false || (typeof (exampleObj.strict) == "object" && exampleObj.strict.value === false)) { | ||
return; | ||
} | ||
if (v[x].strict && typeof v[x].strict != "boolean") { | ||
var s = ts.error(messageRegistry.STRICT_BOOLEAN, _this); | ||
s.setValidationPath({ name: x, child: { name: "strict", child: { name: "strict" } } }); | ||
return s; | ||
} | ||
} | ||
@@ -433,12 +474,2 @@ var example = parseExampleIfNeeded(val, _this.owner()); | ||
}); | ||
if (typeof v[x] == "object" && v[x].value) { | ||
Object.keys(v[x]).forEach(function (key) { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), v[x][key]); | ||
var aRes = a.validateSelf(registry, true); | ||
examplesPatchPath(aRes, true, x); | ||
rs.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -453,2 +484,32 @@ }); | ||
}; | ||
Examples.prototype.checkScalarProperty = function (exampleObj, exampleName, y, registry, status) { | ||
var propName = y.propName; | ||
var propType = y.propType; | ||
var propObj = exampleObj[propName]; | ||
if (propObj && typeof propObj != propType) { | ||
var vp = null; | ||
if (typeof (propObj) == "object") { | ||
vp = { name: "value" }; | ||
Object.keys(propObj).forEach(function (key) { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), exampleObj[propName][key]); | ||
var aRes = a.validateSelf(registry, true); | ||
ts.setValidationPath(aRes, { | ||
name: "examples", | ||
child: { name: exampleName, child: { name: propName, child: { name: key } } } | ||
}); | ||
status.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
if (!propObj.value && typeof (propObj.value) != propType) { | ||
var s = ts.error(y.messageEntry, this); | ||
ts.setValidationPath(s, { | ||
name: "examples", | ||
child: { name: exampleName, child: { name: propName, child: vp } } | ||
}); | ||
status.addSubStatus(s); | ||
} | ||
} | ||
}; | ||
Examples.prototype.kind = function () { | ||
@@ -462,6 +523,6 @@ return tsInterfaces.MetaInformationKind.Examples; | ||
if (noVal) { | ||
example.setValidationPath({ name: "examples", child: { name: x } }); | ||
ts.setValidationPath(example, { name: "examples", child: { name: x } }); | ||
} | ||
else { | ||
example.setValidationPath({ name: "examples", child: { name: x, child: { name: "value" } } }); | ||
ts.setValidationPath(example, { name: "examples", child: { name: x, child: { name: "value" } } }); | ||
} | ||
@@ -486,7 +547,9 @@ } | ||
Default.prototype.validateSelf = function (registry) { | ||
var result = _super.prototype.validateSelf.call(this, registry); | ||
var valOwner = this.owner().validateDirect(this.value(), true); | ||
if (!valOwner.isOk()) { | ||
return ts.error(messageRegistry.INVALID_DEFAULT_VALUE, this, { msg: valOwner.getMessage() }); | ||
result = ts.error(messageRegistry.INVALID_DEFAULT_VALUE, this, { msg: valOwner.getMessage() }); | ||
ts.setValidationPath(result, { name: this.facetName() }); | ||
} | ||
return ts.ok(); | ||
return result; | ||
}; | ||
@@ -514,3 +577,3 @@ Default.prototype.kind = function () { | ||
var _this = this; | ||
var result = ts.ok(); | ||
var result = _super.prototype.validateSelf.call(this, registry); | ||
if (this.owner().isUnion()) { | ||
@@ -534,3 +597,5 @@ result = ts.error(messageRegistry.DISCRIMINATOR_FOR_UNION, this); | ||
} | ||
result.setValidationPath({ name: this.facetName() }); | ||
if (!result.getValidationPath()) { | ||
ts.setValidationPath(result, { name: this.facetName() }); | ||
} | ||
return result; | ||
@@ -577,3 +642,3 @@ }; | ||
//var wrng = new Status(Status.WARNING, Status.CODE_INCORRECT_DISCRIMINATOR, dVal, this); | ||
wrng.setValidationPath({ name: dName, child: path }); | ||
ts.setValidationPath(wrng, { name: dName, child: path }); | ||
return wrng; | ||
@@ -589,3 +654,3 @@ } | ||
//var err = new Status(Status.ERROR, Status.CODE_MISSING_DISCRIMINATOR, dVal, this); | ||
err.setValidationPath(path); | ||
ts.setValidationPath(err, path); | ||
return err; | ||
@@ -597,25 +662,30 @@ } | ||
DiscriminatorValue.prototype.validateSelf = function (registry) { | ||
if (!this.strict) { | ||
return ts.ok(); | ||
var st = _super.prototype.validateSelf.call(this, registry); | ||
if (this.strict) { | ||
var ds = this.owner().oneMeta(Discriminator); | ||
if (!this.owner().isSubTypeOf(ts.OBJECT)) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_FOR_OBJECT, this)); | ||
} | ||
else if (this.owner().getExtra(ts.GLOBAL) === false) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_FOR_INLINE, this)); | ||
} | ||
else if (!ds) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_VALUE_WITHOUT_DISCRIMINATOR, this)); | ||
} | ||
else { | ||
var prop = _.find(this.owner().meta(), function (x) { | ||
return x instanceof restrictions_1.PropertyIs && x.propertyName() == ds.value(); | ||
}); | ||
if (prop) { | ||
var sm = prop.value().validate(this.value()); | ||
if (!sm.isOk()) { | ||
st.addSubStatus(ts.error(messageRegistry.INVALID_DISCRIMINATOR_VALUE, this, { msg: sm.getMessage() })); | ||
} | ||
} | ||
} | ||
} | ||
if (!this.owner().isSubTypeOf(ts.OBJECT)) { | ||
return ts.error(messageRegistry.DISCRIMINATOR_FOR_OBJECT, this); | ||
if (!st.getValidationPath()) { | ||
ts.setValidationPath(st, { name: this.facetName() }); | ||
} | ||
if (this.owner().getExtra(ts.GLOBAL) === false) { | ||
return ts.error(messageRegistry.DISCRIMINATOR_FOR_INLINE, this); | ||
} | ||
var ds = this.owner().oneMeta(Discriminator); | ||
if (!ds) { | ||
return ts.error(messageRegistry.DISCRIMINATOR_VALUE_WITHOUT_DISCRIMINATOR, this); | ||
} | ||
var prop = _.find(this.owner().meta(), function (x) { | ||
return x instanceof restrictions_1.PropertyIs && x.propertyName() == ds.value(); | ||
}); | ||
if (prop) { | ||
var sm = prop.value().validate(this.value()); | ||
if (!sm.isOk()) { | ||
return ts.error(messageRegistry.INVALID_DISCRIMINATOR_VALUE, this, { msg: sm.getMessage() }); | ||
} | ||
} | ||
return ts.ok(); | ||
return st; | ||
}; | ||
@@ -622,0 +692,0 @@ DiscriminatorValue.prototype.requiredType = function () { |
@@ -92,4 +92,11 @@ import tsInterfaces = require("./typesystem-interfaces"); | ||
displayName(): string; | ||
annotations(): any; | ||
annotations(): { | ||
[aName: string]: tsInterfaces.IAnnotation; | ||
}; | ||
name(): string; | ||
scalarsAnnotations(): { | ||
[pName: string]: { | ||
[aName: string]: tsInterfaces.IAnnotation; | ||
}; | ||
}; | ||
} | ||
@@ -96,0 +103,0 @@ export declare class ValueRequirement { |
import ti = require("./nominal-interfaces"); | ||
import tsInterfaces = require("./typesystem-interfaces"); | ||
export declare type IAnnotation = ti.IAnnotation; | ||
@@ -68,4 +69,4 @@ export declare type ITypeDefinition = ti.ITypeDefinition; | ||
addFacet(q: IProperty): void; | ||
_validator: (x: any) => ti.Status[]; | ||
validate(x: any): ti.Status[]; | ||
_validator: (x: any) => tsInterfaces.IStatus[]; | ||
validate(x: any): tsInterfaces.IStatus[]; | ||
allFacets(ps?: { | ||
@@ -72,0 +73,0 @@ [name: string]: ITypeDefinition; |
@@ -325,3 +325,4 @@ "use strict"; | ||
var ff = x.getFixedFacets(); | ||
for (var key in Object.keys(ff)) { | ||
for (var _i = 0, _a = Object.keys(ff); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
mm[key] = ff[key]; | ||
@@ -328,0 +329,0 @@ } |
@@ -140,3 +140,3 @@ "use strict"; | ||
} | ||
vs._validator = function (x) { return t.validate(x, false).getErrors(); }; | ||
vs._validator = getValidator(t); | ||
if (t.isBuiltin()) { | ||
@@ -153,2 +153,7 @@ vs.buildIn = true; | ||
exports.toNominal = toNominal; | ||
function getValidator(t) { | ||
return function (arg) { | ||
return t.validate(arg, false).getErrors(); | ||
}; | ||
} | ||
//# sourceMappingURL=nominals.js.map |
@@ -632,2 +632,15 @@ "use strict"; | ||
} | ||
function appendAnnotations(appendedInfo, childNode) { | ||
var children = childNode.children(); | ||
for (var _i = 0, children_1 = children; _i < children_1.length; _i++) { | ||
var ch = children_1[_i]; | ||
var key = ch.key(); | ||
if (key && key.charAt(0) == "(" && key.charAt(key.length - 1) == ")") { | ||
var aName = key.substring(1, key.length - 1); | ||
var aInstance = new meta.Annotation(aName, ch.value()); | ||
aInstance.setOwnerFacet(appendedInfo); | ||
appendedInfo.addAnnotation(aInstance); | ||
} | ||
} | ||
} | ||
/** | ||
@@ -701,2 +714,3 @@ * parses a type from a JSON structure | ||
} | ||
var typePropAnnotations = []; | ||
if (!tp || ignoreTypeAttr) { | ||
@@ -721,2 +735,14 @@ if (defaultsToAny) { | ||
else { | ||
var sAnnotations = []; | ||
var actual = tp.childWithKey("value"); | ||
if (actual && (actual.kind() == NodeKind.SCALAR || actual.kind() == NodeKind.ARRAY)) { | ||
sAnnotations = [tp.children().filter(function (x) { | ||
var key = x.key(); | ||
if (!key) { | ||
return false; | ||
} | ||
return key.charAt(0) == "(" && key.charAt(key.length - 1) == ")"; | ||
})]; | ||
tp = actual; | ||
} | ||
if (tp.kind() == NodeKind.SCALAR) { | ||
@@ -732,3 +758,19 @@ var valString = tp.value(); | ||
else if (tp.kind() == NodeKind.ARRAY) { | ||
superTypes = tp.children().map(function (x) { return x.value(); }).map(function (y) { return typeExpressions.parseToType("" + y, r, provider); }); | ||
superTypes = tp.children().map(function (x) { | ||
var actual = x.childWithKey("value"); | ||
if (actual && (actual.kind() == NodeKind.SCALAR || actual.kind() == NodeKind.ARRAY)) { | ||
sAnnotations.push(x.children().filter(function (x) { | ||
var key = x.key(); | ||
if (!key) { | ||
return false; | ||
} | ||
return key.charAt(0) == "(" && key.charAt(key.length - 1) == ")"; | ||
})); | ||
x = actual; | ||
} | ||
else { | ||
sAnnotations.push([]); | ||
} | ||
return x.value(); | ||
}).map(function (y) { return typeExpressions.parseToType("" + y, r, provider); }); | ||
} | ||
@@ -738,4 +780,22 @@ else if (tp.kind() == NodeKind.MAP) { | ||
} | ||
if (sAnnotations.length > 0 && sAnnotations.filter(function (x) { return x.length > 0; }).length > 0) { | ||
for (var _i = 0, sAnnotations_1 = sAnnotations; _i < sAnnotations_1.length; _i++) { | ||
var aArr = sAnnotations_1[_i]; | ||
var aiArr = []; | ||
typePropAnnotations.push(aiArr); | ||
for (var _a = 0, aArr_1 = aArr; _a < aArr_1.length; _a++) { | ||
var ann = aArr_1[_a]; | ||
var key = ann.key(); | ||
var aName = key.substring(1, key.length - 1); | ||
var aInstance = new meta.Annotation(aName, ann.value()); | ||
aiArr.push(aInstance); | ||
} | ||
} | ||
} | ||
} | ||
var result = ts.derive(name, superTypes); | ||
for (var i = 0; i < typePropAnnotations.length; i++) { | ||
var aArr1 = typePropAnnotations[i]; | ||
result.addSupertypeAnnotation(aArr1, i); | ||
} | ||
if (r instanceof AccumulatingRegistry) { | ||
@@ -746,4 +806,9 @@ result = contributeToAccumulatingRegistry(result, r); | ||
var hasfacetsOrOtherStuffDoesNotAllowedInExternals = null; | ||
n.children().forEach(function (x) { | ||
var key = x.key(); | ||
n.children().forEach(function (childNode) { | ||
var key = childNode.key(); | ||
actual = childNode.childWithKey("value"); | ||
var x = childNode; | ||
if (key != "example" && actual) { | ||
x = actual; | ||
} | ||
if (!key) { | ||
@@ -767,37 +832,47 @@ return; | ||
} | ||
var appendedInfo; | ||
if (key == "items") { | ||
if (result.isSubTypeOf(ts.ARRAY)) { | ||
var tp = parse(null, x, r, false, false, false); | ||
actualResult.addMeta(new restrictions_1.ComponentShouldBeOfType(tp)); | ||
appendedInfo = new restrictions_1.ComponentShouldBeOfType(tp); | ||
actualResult.addMeta(appendedInfo); | ||
actualResult.putExtra(tsInterfaces.HAS_ITEMS, true); | ||
return; | ||
if (actual) { | ||
appendAnnotations(appendedInfo, childNode); | ||
} | ||
return appendedInfo; | ||
} | ||
} | ||
if (key === "facets") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
return; | ||
} | ||
if (key == "default" || key == "xml" || key == "required") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
} | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
result.addMeta(new meta.Annotation(key.substr(1, key.length - 2), x.value())); | ||
return; | ||
} | ||
var vl = facetR.getInstance().buildFacet(key, x.value()); | ||
//TODO remove "format" condition and use this check for all facets | ||
if (vl && (key != "format" || testFacetAgainstType(vl, result))) { | ||
vl.setNode(x); | ||
result.addMeta(vl); | ||
} | ||
else { | ||
if (annotation && key === "allowedTargets") { | ||
result.addMeta(new meta.AllowedTargets(x.value())); | ||
if (key === "facets") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
return; | ||
} | ||
else if (key == "default" || key == "xml" || key == "required") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
} | ||
else if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
result.addMeta(new meta.Annotation(key.substr(1, key.length - 2), x.value())); | ||
return; | ||
} | ||
appendedInfo = facetR.getInstance().buildFacet(key, x.value()); | ||
//TODO remove "format" condition and use this check for all facets | ||
if (appendedInfo && (key != "format" || testFacetAgainstType(appendedInfo, result))) { | ||
appendedInfo.setNode(x); | ||
result.addMeta(appendedInfo); | ||
} | ||
else { | ||
var customFacet = new meta.CustomFacet(key, x.value()); | ||
customFacet.setNode(x); | ||
result.addMeta(customFacet); | ||
if (annotation && key === "allowedTargets") { | ||
result.addMeta(new meta.AllowedTargets(x.value())); | ||
} | ||
else { | ||
var customFacet = new meta.CustomFacet(key, x.value()); | ||
customFacet.setNode(x); | ||
result.addMeta(customFacet); | ||
} | ||
} | ||
} | ||
if (appendedInfo) { | ||
appendAnnotations(appendedInfo, childNode); | ||
} | ||
}); | ||
@@ -826,2 +901,6 @@ if (result.metaOfType(meta.DiscriminatorValue).length == 0) { | ||
if (ap) { | ||
actual = ap.childWithKey("value"); | ||
if (actual) { | ||
ap = actual; | ||
} | ||
result.addMeta(new restrictions_2.KnownPropertyRestriction(ap.value())); | ||
@@ -828,0 +907,0 @@ } |
@@ -59,3 +59,3 @@ "use strict"; | ||
var s = ts.error(messageRegistry.VALIDATING_AGAINS_UNKNOWN, this, { typeName: t.name() }); | ||
s.setValidationPath(this.patchPath(q)); | ||
ts.setValidationPath(s, this.patchPath(q)); | ||
return s; | ||
@@ -65,3 +65,3 @@ } | ||
st.getErrors().forEach(function (x) { return s.addSubStatus(x); }); | ||
s.setValidationPath(this.patchPath(q)); | ||
ts.setValidationPath(s, this.patchPath(q)); | ||
return s; | ||
@@ -74,3 +74,3 @@ } | ||
var p = ts.error(messageRegistry.EXTERNAL_IN_PROPERTY_DEFINITION, this); | ||
p.setValidationPath({ name: this.propId() }); | ||
ts.setValidationPath(p, { name: this.propId() }); | ||
return p; | ||
@@ -84,3 +84,3 @@ } | ||
}); | ||
p.setValidationPath({ name: this.propId(), child: { name: "type" } }); | ||
ts.setValidationPath(p, { name: this.propId(), child: { name: "type" } }); | ||
return p; | ||
@@ -96,3 +96,3 @@ } | ||
st.getErrors().forEach(function (y) { p.addSubStatus(y); }); | ||
p.setValidationPath({ name: this.propId() }); | ||
ts.setValidationPath(p, { name: this.propId() }); | ||
return p; | ||
@@ -109,3 +109,3 @@ } | ||
}); | ||
p.setValidationPath({ name: this.propId() }); | ||
ts.setValidationPath(p, { name: this.propId() }); | ||
return p; | ||
@@ -219,3 +219,3 @@ } | ||
var err = ts.error(messageRegistry.UNKNOWN_PROPERTY, _this, { propName: x }); | ||
err.setValidationPath({ name: x }); | ||
ts.setValidationPath(err, { name: x }); | ||
s.addSubStatus(err); | ||
@@ -622,2 +622,3 @@ }); | ||
var _this = this; | ||
var superStatus = _super.prototype.validateSelf.call(this, registry); | ||
var ownerIsCorrect = false; | ||
@@ -636,2 +637,3 @@ if (this.checkOwner(this.requiredType())) { | ||
} | ||
var rs; | ||
if (!ownerIsCorrect) { | ||
@@ -646,11 +648,25 @@ var typeNames = this.requiredType().name(); | ||
}); | ||
rs.setValidationPath({ name: this.facetName() }); | ||
return rs; | ||
} | ||
var rs = this.checkValue(); | ||
if (rs) { | ||
rs.setValidationPath({ name: this.facetName() }); | ||
else { | ||
rs = this.checkValue(); | ||
} | ||
if (rs && !rs.isOk()) { | ||
ts.setValidationPath(rs, { name: this.facetName() }); | ||
return rs; | ||
} | ||
return ts.ok(); | ||
var statuses = [superStatus, rs].filter(function (x) { return x && !x.isOk(); }); | ||
if (statuses.length == 0) { | ||
return ts.ok(); | ||
} | ||
else if (statuses.length == 1) { | ||
return statuses[0]; | ||
} | ||
else { | ||
var result = ts.ok(); | ||
for (var _i = 0, statuses_1 = statuses; _i < statuses_1.length; _i++) { | ||
var status = statuses_1[_i]; | ||
result.addSubStatus(status); | ||
} | ||
return result; | ||
} | ||
}; | ||
@@ -1015,2 +1031,5 @@ return FacetRestriction; | ||
UniqueItems.prototype.checkValue = function () { | ||
if (typeof (this._value) != "boolean") { | ||
return ts.error(messageRegistry.UNIQUE_ITEMS_BOOLEAN, this); | ||
} | ||
return null; | ||
@@ -1051,3 +1070,3 @@ }; | ||
} | ||
ss.setValidationPath({ name: "" + j }); | ||
ts.setValidationPath(ss, { name: "" + j }); | ||
rs.addSubStatus(ss); | ||
@@ -1059,6 +1078,7 @@ } | ||
ComponentShouldBeOfType.prototype.validateSelf = function (registry) { | ||
var st = _super.prototype.validateSelf.call(this, registry); | ||
if (this.type.isAnonymous()) { | ||
var st = this.type.validateType(registry); | ||
if (!st.isOk()) { | ||
return ts.error(messageRegistry.INVALID_COMPONENT_TYPE, this, { msg: st.getMessage() }); | ||
var typeStatus = this.type.validateType(registry); | ||
if (!typeStatus.isOk()) { | ||
st.addSubStatus(ts.error(messageRegistry.INVALID_COMPONENT_TYPE, this, { msg: st.getMessage() })); | ||
} | ||
@@ -1068,14 +1088,14 @@ return st; | ||
if (this.type.isExternal()) { | ||
return ts.error(messageRegistry.EXTERNAL_AS_COMPONENT, this); | ||
st.addSubStatus(ts.error(messageRegistry.EXTERNAL_AS_COMPONENT, this)); | ||
} | ||
if (this.type.isSubTypeOf(ts.UNKNOWN) || this.type.isSubTypeOf(ts.RECURRENT)) { | ||
return ts.error(messageRegistry.UNKNOWN_AS_COMPONENT, this, { typeName: this.type.name() }); | ||
else if (this.type.isSubTypeOf(ts.UNKNOWN) || this.type.isSubTypeOf(ts.RECURRENT)) { | ||
st.addSubStatus(ts.error(messageRegistry.UNKNOWN_AS_COMPONENT, this, { typeName: this.type.name() })); | ||
} | ||
if (this.type.isUnion()) { | ||
else if (this.type.isUnion()) { | ||
var ui = _.find(this.type.typeFamily(), function (x) { return x.isSubTypeOf(ts.UNKNOWN); }); | ||
if (ui) { | ||
return ts.error(messageRegistry.UNKNOWN_AS_COMPONENT, this, { typeName: ui.name() }); | ||
st.addSubStatus(ts.error(messageRegistry.UNKNOWN_AS_COMPONENT, this, { typeName: ui.name() })); | ||
} | ||
} | ||
return ts.ok(); | ||
return st; | ||
}; | ||
@@ -1082,0 +1102,0 @@ ComponentShouldBeOfType.prototype.composeWith = function (t) { |
export interface IValidationPath { | ||
name: string; | ||
name: string | number; | ||
child?: IValidationPath; | ||
@@ -21,3 +21,3 @@ } | ||
/** | ||
* retur2ns true if status does not have errors | ||
* returns true if status does not have errors | ||
*/ | ||
@@ -34,5 +34,10 @@ isOk(): boolean; | ||
/** | ||
* return true if this status is just information | ||
*/ | ||
isInfo(): boolean; | ||
/** | ||
* returns human readable message associated with this status | ||
*/ | ||
getMessage(): string; | ||
setMessage(m: string): void; | ||
/** | ||
@@ -51,2 +56,3 @@ * returns an array of nested statuses | ||
getValidationPath(): IValidationPath; | ||
setValidationPath(p: IValidationPath): void; | ||
/** | ||
@@ -60,2 +66,4 @@ * returns path to this status | ||
getCode(): string; | ||
setCode(c: string): void; | ||
getSeverity(): number; | ||
} | ||
@@ -115,3 +123,20 @@ export declare enum MetaInformationKind { | ||
kind(): MetaInformationKind; | ||
/** | ||
* Annotations applied to the facet | ||
*/ | ||
annotations(): IAnnotation[]; | ||
} | ||
/** | ||
* Model of annotation instances applied to types or their facets | ||
*/ | ||
export interface IAnnotation extends ITypeFacet { | ||
/** | ||
* Returns owner facet for annotations applied to facets | ||
*/ | ||
ownerFacet(): ITypeFacet; | ||
/** | ||
* Returns owner type for annotations applied to types | ||
*/ | ||
owner(): IParsedType; | ||
} | ||
export interface IParsedTypeCollection { | ||
@@ -272,1 +297,91 @@ /** | ||
} | ||
/** | ||
* A model of custom type validation plugin | ||
*/ | ||
export interface ITypeValidationPlugin { | ||
/** | ||
* @param t the type to be validated | ||
* @param reg context type registry | ||
*/ | ||
process(t: IParsedType, reg: ITypeRegistry): PluginValidationIssue[]; | ||
/** | ||
* String ID of the plugin | ||
*/ | ||
id(): string; | ||
} | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
export declare function getTypeValidationPlugins(): ITypeValidationPlugin[]; | ||
/** | ||
* Model of annotation instance used as input fo validation plugins | ||
*/ | ||
export interface IAnnotationInstance { | ||
/** | ||
* Annotation name | ||
*/ | ||
name(): string; | ||
/** | ||
* Annotation value | ||
*/ | ||
value(): any; | ||
/** | ||
* Annotation definition type | ||
*/ | ||
definition(): IParsedType; | ||
} | ||
export interface PluginValidationIssue { | ||
issueCode?: string; | ||
message?: string; | ||
isWarning?: boolean; | ||
path?: IValidationPath; | ||
} | ||
/** | ||
* Model of annotation validator for typesystem | ||
*/ | ||
export interface IAnnotationValidationPlugin { | ||
/** | ||
* validate annotated RAML element | ||
*/ | ||
process(entry: IAnnotatedElement): PluginValidationIssue[]; | ||
/** | ||
* String ID of the plugin | ||
*/ | ||
id(): string; | ||
} | ||
/** | ||
* A model of annotated RAML element used as input for | ||
* annotation validation plugins | ||
*/ | ||
export interface IAnnotatedElement { | ||
/** | ||
* Element kind | ||
*/ | ||
kind(): string; | ||
/** | ||
* Map view on the annotations applied | ||
*/ | ||
annotationsMap(): { | ||
[key: string]: IAnnotationInstance; | ||
}; | ||
/** | ||
* Array view on the annotations applied | ||
*/ | ||
annotations(): IAnnotationInstance[]; | ||
/** | ||
* JSON representation of the entry | ||
*/ | ||
value(): any; | ||
/** | ||
* Element name | ||
*/ | ||
name(): string; | ||
/** | ||
* The element itself | ||
*/ | ||
entry(): any; | ||
} | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
export declare function getAnnotationValidationPlugins(): IAnnotationValidationPlugin[]; |
@@ -33,2 +33,30 @@ "use strict"; | ||
var MetaInformationKind = exports.MetaInformationKind; | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
function getTypeValidationPlugins() { | ||
var rv = global.ramlValidation; | ||
if (rv) { | ||
var typeValidators = rv.typeValidators; | ||
if (Array.isArray(typeValidators)) { | ||
return typeValidators; | ||
} | ||
} | ||
return []; | ||
} | ||
exports.getTypeValidationPlugins = getTypeValidationPlugins; | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
function getAnnotationValidationPlugins() { | ||
var rv = global.ramlValidation; | ||
if (rv) { | ||
var typesystemAnnotationValidators = rv.typesystemAnnotationValidators; | ||
if (Array.isArray(typesystemAnnotationValidators)) { | ||
return typesystemAnnotationValidators; | ||
} | ||
} | ||
return []; | ||
} | ||
exports.getAnnotationValidationPlugins = getAnnotationValidationPlugins; | ||
//# sourceMappingURL=typesystem-interfaces.js.map |
/// <reference path="../../typings/main.d.ts" /> | ||
import su = require("./schemaUtil"); | ||
import tsInterfaces = require("./typesystem-interfaces"); | ||
import { ParseNode } from "./parse"; | ||
export declare let messageRegistry: any; | ||
@@ -20,12 +19,11 @@ export declare type IValidationPath = tsInterfaces.IValidationPath; | ||
protected source: any; | ||
protected subStatus: Status[]; | ||
protected subStatus: tsInterfaces.IStatus[]; | ||
protected vp: tsInterfaces.IValidationPath; | ||
getValidationPath(): tsInterfaces.IValidationPath; | ||
getValidationPathAsString(): string; | ||
patchPath(p: tsInterfaces.IValidationPath): tsInterfaces.IValidationPath; | ||
setValidationPath(_c: tsInterfaces.IValidationPath): void; | ||
constructor(severity: number, code: string, message: string, source: any, takeNodeFromSource?: boolean); | ||
addSubStatus(st: Status, pathName?: string): void; | ||
getErrors(): Status[]; | ||
getSubStatuses(): Status[]; | ||
addSubStatus(st: tsInterfaces.IStatus, pathName?: string): void; | ||
getErrors(): tsInterfaces.IStatus[]; | ||
getSubStatuses(): tsInterfaces.IStatus[]; | ||
getSeverity(): number; | ||
@@ -56,5 +54,6 @@ getMessage(): string; | ||
_owner: AbstractType; | ||
_node: ParseNode; | ||
node(): ParseNode; | ||
setNode(node: ParseNode): void; | ||
_node: parse.ParseNode; | ||
_annotations: tsInterfaces.IAnnotation[]; | ||
node(): parse.ParseNode; | ||
setNode(node: parse.ParseNode): void; | ||
owner(): AbstractType; | ||
@@ -75,2 +74,4 @@ isInheritable(): boolean; | ||
abstract kind(): tsInterfaces.MetaInformationKind; | ||
annotations(): tsInterfaces.IAnnotation[]; | ||
addAnnotation(a: tsInterfaces.IAnnotation): void; | ||
} | ||
@@ -146,2 +147,5 @@ export declare abstract class Constraint extends TypeInformation { | ||
}; | ||
protected supertypeAnnotations: { | ||
[aName: string]: tsInterfaces.IAnnotation; | ||
}[]; | ||
getExtra(name: string): any; | ||
@@ -171,2 +175,3 @@ putExtra(name: string, v: any): void; | ||
superTypes(): AbstractType[]; | ||
addSupertypeAnnotation(arr: tsInterfaces.IAnnotation[], ind: number): void; | ||
validateType(tr?: TypeRegistry): Status; | ||
@@ -541,1 +546,92 @@ validateHierarchy(rs: Status): void; | ||
} | ||
export declare function setValidationPath(_s: tsInterfaces.IStatus, _c: tsInterfaces.IValidationPath): void; | ||
export declare function patchPath(p: tsInterfaces.IValidationPath): tsInterfaces.IValidationPath; | ||
/** | ||
* A model of annotated RAML type facet | ||
*/ | ||
export declare class AnnotatedFacet implements tsInterfaces.IAnnotatedElement { | ||
protected _facet: TypeInformation; | ||
protected reg: tsInterfaces.ITypeRegistry; | ||
constructor(_facet: TypeInformation, reg: tsInterfaces.ITypeRegistry); | ||
kind(): string; | ||
private _annotations; | ||
private _annotationsMap; | ||
annotationsMap(): { | ||
[key: string]: tsInterfaces.IAnnotationInstance; | ||
}; | ||
annotations(): tsInterfaces.IAnnotationInstance[]; | ||
/** | ||
* Value of the facet serialized to JSON | ||
*/ | ||
value(): any; | ||
/** | ||
* Facet name | ||
*/ | ||
name(): string; | ||
/** | ||
* The facet itself | ||
*/ | ||
entry(): tsInterfaces.ITypeFacet; | ||
} | ||
import parse = require("./parse"); | ||
/** | ||
* A model of annotated RAML type | ||
*/ | ||
export declare class AnnotatedType implements tsInterfaces.IAnnotatedElement { | ||
private _type; | ||
protected reg: tsInterfaces.ITypeRegistry; | ||
constructor(_type: AbstractType, reg: tsInterfaces.ITypeRegistry); | ||
private _annotations; | ||
private _annotationsMap; | ||
kind(): string; | ||
annotationsMap(): { | ||
[key: string]: tsInterfaces.IAnnotationInstance; | ||
}; | ||
annotations(): tsInterfaces.IAnnotationInstance[]; | ||
/** | ||
* JSON representation of the type | ||
*/ | ||
value(): any; | ||
/** | ||
* Type name | ||
*/ | ||
name(): string; | ||
/** | ||
* The type itself | ||
* @returns {IParsedType} | ||
*/ | ||
entry(): tsInterfaces.IParsedType; | ||
} | ||
export declare class AnnotationInstance implements tsInterfaces.IAnnotationInstance { | ||
protected actual: tsInterfaces.IAnnotation; | ||
constructor(actual: tsInterfaces.IAnnotation, reg: tsInterfaces.ITypeRegistry); | ||
name(): string; | ||
/** | ||
* Annotation value | ||
*/ | ||
value(): any; | ||
/** | ||
* Annotation definition type | ||
*/ | ||
definition(): tsInterfaces.IParsedType; | ||
/** | ||
* Actual annotation model | ||
*/ | ||
annotation(): tsInterfaces.IAnnotation; | ||
} | ||
/** | ||
* Apply registered type validation plugins to the type | ||
* @param t type to be validated | ||
* @param reg context type registry | ||
* @param skipOk whether to omit OK issues | ||
* @returns an array of {tsInterfaces.IStatus} | ||
*/ | ||
export declare function applyAnnotationValidationPlugins(e: tsInterfaces.IAnnotatedElement): Status[]; | ||
/** | ||
* Apply registered type validation plugins to the type | ||
* @param t type to be validated | ||
* @param reg context type registry | ||
* @param skipOk whether to omit OK issues | ||
* @returns an array of {tsInterfaces.IStatus} | ||
*/ | ||
export declare function applyTypeValidationPlugins(t: tsInterfaces.IParsedType, reg: tsInterfaces.ITypeRegistry): Status[]; |
@@ -429,3 +429,3 @@ "use strict"; | ||
var st = t.validateType(ts.builtInRegistry()); | ||
assert.isTrue(st.getErrors().length === 0); | ||
assert.isTrue(st.getErrors().length === 2); | ||
}); | ||
@@ -432,0 +432,0 @@ it("properties is map", function () { |
{ | ||
"name": "raml-typesystem", | ||
"version": "0.0.58", | ||
"version": "0.0.59", | ||
"main": "dist/src/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -174,2 +174,10 @@ { | ||
}, | ||
"DISPLAY_NAME_STRING" : { | ||
"code" : "DISPLAY_NAME_STRING", | ||
"message": "'displayName' should be string" | ||
}, | ||
"DESCRIPTION_STRING" : { | ||
"code" : "DESCRIPTION_STRING", | ||
"message": "'description' should be string" | ||
}, | ||
"INVALID_EXMAPLE" : { | ||
@@ -183,2 +191,6 @@ "code" : "INVALID_EXMAPLE", | ||
}, | ||
"UNIQUE_ITEMS_BOOLEAN" : { | ||
"code" : "UNIQUE_ITEMS_BOOLEAN", | ||
"message": "Value of 'uniqueItems' facet should be boolean" | ||
}, | ||
"EXMAPLES_MAP" : { | ||
@@ -185,0 +197,0 @@ "code" : "EXMAPLES_MAP", |
@@ -81,3 +81,3 @@ import rt=require("./typesystem") | ||
private _strict:boolean=true, | ||
private _annotations:any=undefined, | ||
private _annotations:{[key:string]:meta.Annotation}={}, | ||
private _isSingle:boolean=false, | ||
@@ -88,2 +88,5 @@ private _empty:boolean=false){ | ||
private _scalarsAnnotations: | ||
{[pName:string]:{[aName:string]:meta.Annotation}} = {}; | ||
isEmpty():boolean { | ||
@@ -169,3 +172,3 @@ return this._empty; | ||
annotations():any{ | ||
annotations():{[key:string]:meta.Annotation}{ | ||
return this._annotations; | ||
@@ -178,2 +181,15 @@ } | ||
scalarsAnnotations():{[pName:string]:{[aName:string]:meta.Annotation}}{ | ||
return this._scalarsAnnotations; | ||
} | ||
registerScalarAnnotatoion(a:meta.Annotation,pName:string){ | ||
var aMap = this._scalarsAnnotations[pName]; | ||
if(!aMap){ | ||
aMap = {}; | ||
this._scalarsAnnotations[pName] = aMap; | ||
} | ||
aMap[a.facetName()] = a; | ||
} | ||
} | ||
@@ -189,16 +205,19 @@ var toExample = function (owner: any, exampleObj:any, name:string=null,isSingle:boolean=false) { | ||
else { | ||
var displayName = exampleObj.displayName; | ||
var description = exampleObj.description; | ||
var strict:boolean = exampleObj.strict; | ||
var aObj:any = null; | ||
var annotationNames = Object.keys(exampleObj).filter(x=>x.charAt(0) == "("); | ||
if (annotationNames.length > 0) { | ||
aObj = {}; | ||
for (var aName of annotationNames) { | ||
var aVal = exampleObj[aName]; | ||
aName = aName.substring(1, aName.length - 1); | ||
aObj[aName] = aVal; | ||
} | ||
var displayName = scalarValue(exampleObj, "displayName"); | ||
var description = scalarValue(exampleObj, "description"); | ||
var strict:boolean = scalarValue(exampleObj, "strict"); | ||
var aObj:{[key:string]:meta.Annotation} = {}; | ||
scalarAnnotaitons(exampleObj).forEach(x=>{ | ||
aObj[x.facetName()] = x; | ||
}); | ||
example = new Example(val, name, displayName, description, strict, aObj, isSingle); | ||
for(var a of scalarAnnotaitons(exampleObj["displayName"])){ | ||
example.registerScalarAnnotatoion(a,"displayName"); | ||
} | ||
example = new Example(val, name, displayName, description, strict, aObj, isSingle); | ||
for(var a of scalarAnnotaitons(exampleObj["description"])){ | ||
example.registerScalarAnnotatoion(a,"description"); | ||
} | ||
for(var a of scalarAnnotaitons(exampleObj["strict"])){ | ||
example.registerScalarAnnotatoion(a,"strict"); | ||
} | ||
} | ||
@@ -214,2 +233,24 @@ } | ||
function scalarValue(obj:any,propName:string):any{ | ||
var pVal = obj[propName]; | ||
if(pVal !=null && typeof(pVal)=="object"){ | ||
return pVal["value"]; | ||
} | ||
return pVal; | ||
} | ||
function scalarAnnotaitons(obj:any):meta.Annotation[]{ | ||
var result:meta.Annotation[] = []; | ||
if(!obj || typeof(obj)!="object"){ | ||
return result; | ||
} | ||
for(var aKey of Object.keys(obj).filter( | ||
x=>x.length>0&&x.charAt(0)=="("&&x.charAt(x.length-1)==")")){ | ||
var aName = aKey.substring(1,aKey.length-1); | ||
var aVal = obj[aKey]; | ||
var a = new meta.Annotation(aName,aVal); | ||
result.push(a); | ||
} | ||
return result; | ||
} | ||
function exampleFromInheritedType(inheritedType:rt.InheritedType) : nm.IExpandableExample[] { | ||
@@ -216,0 +257,0 @@ var result:nm.IExpandableExample[]=[] |
@@ -74,3 +74,3 @@ /// <reference path="../typings/main.d.ts" /> | ||
} | ||
export class Annotation extends MetaInfo{ | ||
export class Annotation extends MetaInfo implements tsInterfaces.IAnnotation{ | ||
@@ -81,2 +81,4 @@ constructor(name: string,value:any){ | ||
private _ownerFacet:tsInterfaces.ITypeFacet; | ||
validateSelf(registry:ts.TypeRegistry,ofExample:boolean=false):ts.Status { | ||
@@ -119,3 +121,3 @@ var tp=registry.get(this.facetName()); | ||
} | ||
result.setValidationPath({name:`(${this.facetName()})`}); | ||
ts.setValidationPath(result,{name:`(${this.facetName()})`}); | ||
return result; | ||
@@ -127,2 +129,10 @@ } | ||
} | ||
ownerFacet(){ | ||
return this._ownerFacet; | ||
} | ||
setOwnerFacet(ownerFacet:tsInterfaces.ITypeFacet){ | ||
this._ownerFacet = ownerFacet; | ||
} | ||
} | ||
@@ -208,2 +218,9 @@ export class FacetDeclaration extends MetaInfo{ | ||
} | ||
var exampleScalarProperties = [ | ||
{propName: "strict", propType: "boolean", messageEntry:messageRegistry.STRICT_BOOLEAN}, | ||
{propName: "displayName", propType: "string", messageEntry:messageRegistry.DISPLAY_NAME_STRING}, | ||
{propName: "description", propType: "string", messageEntry:messageRegistry.DESCRIPTION_STRING} | ||
]; | ||
export class Example extends MetaInfo{ | ||
@@ -218,3 +235,3 @@ constructor(value:any){ | ||
var aStatus = this.validateAnnotations(registry); | ||
aStatus.setValidationPath({name:this.facetName()}); | ||
ts.setValidationPath(aStatus,{name:this.facetName()}); | ||
status.addSubStatus(aStatus); | ||
@@ -227,11 +244,38 @@ return status; | ||
var isVal=false; | ||
var result = ts.ok(); | ||
if (typeof val==="object"&&val){ | ||
if (val.value){ | ||
if (val.strict===false){ | ||
return ts.ok(); | ||
for(var y of exampleScalarProperties) { | ||
var propName = y.propName; | ||
var propType = y.propType; | ||
var propObj = val[propName]; | ||
if (propObj&&typeof propObj!=propType){ | ||
if(typeof(propObj)=="object") { | ||
Object.keys(propObj).forEach(key=> { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), propObj[key]); | ||
var aRes = a.validateSelf(registry, true); | ||
ts.setValidationPath(aRes,{ | ||
name: "example", | ||
child: {name: propName, child: {name: key}} | ||
}); | ||
result.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
if(!propObj.value&&typeof propObj.value!=propType) { | ||
var s = ts.error(y.messageEntry, this); | ||
var vp = propObj.value ? {name: "value"} : null; | ||
ts.setValidationPath(s,{name: "example", child: {name: propName, child: vp}}); | ||
result.addSubStatus(s); | ||
} | ||
} | ||
} | ||
if (val.strict&&typeof val.strict!="boolean"){ | ||
var s= ts.error(messageRegistry.STRICT_BOOLEAN,this); | ||
s.setValidationPath({name: "example", child: {name: "strict"}}) | ||
return s; | ||
if (val.strict===false||(typeof(val.strict)=="object"&&val.strict.value===false)){ | ||
return result; | ||
} | ||
@@ -244,5 +288,6 @@ val=val.value; | ||
var rr=parseExampleIfNeeded(val,this.owner()); | ||
if (rr instanceof ts.Status){ | ||
rr.setValidationPath({name: "example"}) | ||
return rr; | ||
if (rr instanceof ts.Status && !rr.isOk()){ | ||
ts.setValidationPath(rr,{name: "example"}); | ||
result.addSubStatus(rr); | ||
return result; | ||
} | ||
@@ -257,12 +302,12 @@ var valOwner=this.owner().validateDirect(rr,true,false); | ||
if (isVal) { | ||
x.setValidationPath({name: "example", child: {name: "value"}}); | ||
ts.setValidationPath(x,{name: "example", child: {name: "value"}}); | ||
} | ||
else{ | ||
x.setValidationPath({name: "example"}); | ||
ts.setValidationPath(x,{name: "example"}); | ||
} | ||
}); | ||
return c; | ||
result.addSubStatus(c); | ||
} | ||
return ts.ok(); | ||
return result; | ||
} | ||
@@ -321,6 +366,8 @@ | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
var result = super.validateSelf(registry); | ||
if (typeof this.value()!=="boolean"){ | ||
return ts.error(messageRegistry.REQUIRED_BOOLEAN,this); | ||
result = ts.error(messageRegistry.REQUIRED_BOOLEAN,this); | ||
ts.setValidationPath(result,{name:this.facetName()}); | ||
} | ||
return ts.ok(); | ||
return result; | ||
} | ||
@@ -338,6 +385,2 @@ | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
return ts.ok(); | ||
} | ||
kind() : tsInterfaces.MetaInformationKind { | ||
@@ -352,7 +395,2 @@ return tsInterfaces.MetaInformationKind.HasPropertiesFacet; | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
return ts.ok(); | ||
} | ||
kind() : tsInterfaces.MetaInformationKind { | ||
@@ -412,17 +450,28 @@ return tsInterfaces.MetaInformationKind.AllowedTargets; | ||
Object.keys(v).forEach(x=> { | ||
if (v[x]) { | ||
var val=v[x].value; | ||
var exampleObj = v[x]; | ||
if (exampleObj) { | ||
if (typeof exampleObj=="object"&&exampleObj.value) { | ||
Object.keys(exampleObj).forEach(key=> { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), v[x][key]); | ||
var aRes = a.validateSelf(registry,true); | ||
ts.setValidationPath(aRes, | ||
{name:"examples",child:{name: x, child: {name: key}}}); | ||
rs.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
var val=exampleObj.value; | ||
var noVal=!val; | ||
if (noVal){ | ||
val=v[x]; | ||
val=exampleObj; | ||
} | ||
else{ | ||
if (v[x].strict===false){ | ||
for(var y of exampleScalarProperties) { | ||
this.checkScalarProperty(exampleObj, x, y, registry,rs); | ||
} | ||
if (exampleObj.strict===false||( | ||
typeof(exampleObj.strict)=="object" && exampleObj.strict.value === false)){ | ||
return ; | ||
} | ||
if (v[x].strict&&typeof v[x].strict!="boolean"){ | ||
var s= ts.error(messageRegistry.STRICT_BOOLEAN,this); | ||
s.setValidationPath({name: x, child: {name: "strict", child: {name: "strict"}}}); | ||
return s; | ||
} | ||
} | ||
@@ -440,12 +489,2 @@ var example = parseExampleIfNeeded(val, this.owner()); | ||
}); | ||
if (typeof v[x]=="object"&&v[x].value) { | ||
Object.keys(v[x]).forEach(key=> { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), v[x][key]); | ||
var aRes = a.validateSelf(registry,true); | ||
examplesPatchPath(aRes,true,x); | ||
rs.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -461,2 +500,41 @@ }); | ||
private checkScalarProperty( | ||
exampleObj:any, | ||
exampleName:string, | ||
y:any, | ||
registry:ts.TypeRegistry, | ||
status:Status) { | ||
var propName = y.propName; | ||
var propType = y.propType; | ||
var propObj = exampleObj[propName]; | ||
if (propObj && typeof propObj != propType) { | ||
var vp:tsInterfaces.IValidationPath = null; | ||
if (typeof(propObj) == "object") { | ||
vp = {name: "value"}; | ||
Object.keys(propObj).forEach(key=> { | ||
if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
var a = new Annotation(key.substring(1, key.length - 1), exampleObj[propName][key]); | ||
var aRes = a.validateSelf(registry, true); | ||
ts.setValidationPath(aRes, | ||
{ | ||
name: "examples", | ||
child: {name: exampleName, child: {name: propName, child: {name: key}}} | ||
}); | ||
status.addSubStatus(aRes); | ||
} | ||
}); | ||
} | ||
if (!propObj.value && typeof(propObj.value) != propType) { | ||
var s = ts.error(y.messageEntry, this); | ||
ts.setValidationPath(s,{ | ||
name: "examples", | ||
child: {name: exampleName, child: {name: propName, child: vp}} | ||
}); | ||
status.addSubStatus(s); | ||
} | ||
} | ||
} | ||
kind() : tsInterfaces.MetaInformationKind { | ||
@@ -466,8 +544,8 @@ return tsInterfaces.MetaInformationKind.Examples; | ||
} | ||
function examplesPatchPath(example:ts.Status,noVal:boolean,x: string):void{ | ||
function examplesPatchPath(example:tsInterfaces.IStatus,noVal:boolean,x: string):void{ | ||
if (noVal){ | ||
example.setValidationPath({ name: "examples",child:{name: x}}); | ||
ts.setValidationPath(example,{ name: "examples",child:{name: x}}); | ||
} | ||
else { | ||
example.setValidationPath({ name: "examples",child:{name: x, child: {name: "value"}}}); | ||
ts.setValidationPath(example,{ name: "examples",child:{name: x, child: {name: "value"}}}); | ||
} | ||
@@ -493,7 +571,9 @@ } | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
var result = super.validateSelf(registry); | ||
var valOwner=this.owner().validateDirect(this.value(),true); | ||
if (!valOwner.isOk()){ | ||
return ts.error(messageRegistry.INVALID_DEFAULT_VALUE, this , { msg : valOwner.getMessage() }); | ||
result = ts.error(messageRegistry.INVALID_DEFAULT_VALUE, this , { msg : valOwner.getMessage() }); | ||
ts.setValidationPath(result,{name:this.facetName()}); | ||
} | ||
return ts.ok(); | ||
return result; | ||
} | ||
@@ -521,3 +601,3 @@ | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
var result = ts.ok(); | ||
var result = super.validateSelf(registry); | ||
if (this.owner().isUnion()){ | ||
@@ -542,3 +622,5 @@ result = ts.error(messageRegistry.DISCRIMINATOR_FOR_UNION, this); | ||
} | ||
result.setValidationPath({name:this.facetName()}); | ||
if(!result.getValidationPath()) { | ||
ts.setValidationPath(result,{name: this.facetName()}); | ||
} | ||
return result; | ||
@@ -582,3 +664,3 @@ } | ||
//var wrng = new Status(Status.WARNING, Status.CODE_INCORRECT_DISCRIMINATOR, dVal, this); | ||
wrng.setValidationPath({name: dName, child: path}); | ||
ts.setValidationPath(wrng,{name: dName, child: path}); | ||
return wrng; | ||
@@ -594,3 +676,3 @@ } | ||
//var err = new Status(Status.ERROR, Status.CODE_MISSING_DISCRIMINATOR, dVal, this); | ||
err.setValidationPath(path); | ||
ts.setValidationPath(err,path); | ||
return err; | ||
@@ -603,25 +685,30 @@ } | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
if(!this.strict){ | ||
return ts.ok(); | ||
var st = super.validateSelf(registry); | ||
if(this.strict) { | ||
var ds = this.owner().oneMeta(Discriminator); | ||
if (!this.owner().isSubTypeOf(ts.OBJECT)) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_FOR_OBJECT, this)); | ||
} | ||
else if (this.owner().getExtra(ts.GLOBAL) === false) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_FOR_INLINE, this)); | ||
} | ||
else if (!ds) { | ||
st.addSubStatus(ts.error(messageRegistry.DISCRIMINATOR_VALUE_WITHOUT_DISCRIMINATOR, this)); | ||
} | ||
else { | ||
var prop = _.find(this.owner().meta(), x=> | ||
x instanceof PropertyIs && (<PropertyIs>x).propertyName() == ds.value()); | ||
if (prop) { | ||
var sm = prop.value().validate(this.value()); | ||
if (!sm.isOk()) { | ||
st.addSubStatus(ts.error(messageRegistry.INVALID_DISCRIMINATOR_VALUE, | ||
this, {msg: sm.getMessage()})); | ||
} | ||
} | ||
} | ||
} | ||
if (!this.owner().isSubTypeOf(ts.OBJECT)){ | ||
return ts.error(messageRegistry.DISCRIMINATOR_FOR_OBJECT, this); | ||
if(!st.getValidationPath()) { | ||
ts.setValidationPath(st,{name: this.facetName()}); | ||
} | ||
if (this.owner().getExtra(ts.GLOBAL)===false){ | ||
return ts.error(messageRegistry.DISCRIMINATOR_FOR_INLINE, this); | ||
} | ||
var ds=this.owner().oneMeta(Discriminator); | ||
if (!ds){ | ||
return ts.error(messageRegistry.DISCRIMINATOR_VALUE_WITHOUT_DISCRIMINATOR, this); | ||
} | ||
var prop=_.find(this.owner().meta(),x=> | ||
x instanceof PropertyIs&& (<PropertyIs>x).propertyName()==ds.value()); | ||
if (prop){ | ||
var sm=prop.value().validate(this.value()); | ||
if (!sm.isOk()){ | ||
return ts.error(messageRegistry.INVALID_DISCRIMINATOR_VALUE, | ||
this, { msg : sm.getMessage() }); | ||
} | ||
} | ||
return ts.ok(); | ||
return st; | ||
} | ||
@@ -628,0 +715,0 @@ |
@@ -112,5 +112,7 @@ import tsInterfaces = require("./typesystem-interfaces") | ||
annotations():any | ||
annotations():{[aName:string]:tsInterfaces.IAnnotation} | ||
name():string | ||
scalarsAnnotations():{[pName:string]:{[aName:string]:tsInterfaces.IAnnotation}} | ||
} | ||
@@ -117,0 +119,0 @@ export class ValueRequirement{ |
@@ -168,5 +168,5 @@ import ti = require("./nominal-interfaces") | ||
_validator:(x:any)=>ti.Status[] | ||
_validator:(x:any)=>tsInterfaces.IStatus[] | ||
validate(x:any):ti.Status[]{ | ||
validate(x:any):tsInterfaces.IStatus[]{ | ||
if (!this._validator){ | ||
@@ -371,3 +371,3 @@ throw new Error("Validate can be used only on runtime types instances") | ||
var ff = x.getFixedFacets(); | ||
for( var key in Object.keys(ff)){ | ||
for( var key of Object.keys(ff)){ | ||
mm[key] = ff[key]; | ||
@@ -374,0 +374,0 @@ } |
@@ -164,3 +164,3 @@ import ts=require("./typesystem") | ||
} | ||
vs._validator=(x)=>t.validate(x,false).getErrors(); | ||
vs._validator = getValidator(t); | ||
if (t.isBuiltin()){ | ||
@@ -177,1 +177,6 @@ vs.buildIn=true; | ||
function getValidator(t:ts.AbstractType): any { | ||
return function(arg: any) { | ||
return t.validate(arg,false).getErrors(); | ||
} | ||
} |
134
src/parse.ts
@@ -692,2 +692,14 @@ import ts=require("./typesystem"); | ||
function appendAnnotations(appendedInfo:ts.TypeInformation, childNode:ParseNode) { | ||
var children = childNode.children(); | ||
for (var ch of children) { | ||
var key = ch.key(); | ||
if (key && key.charAt(0) == "(" && key.charAt(key.length - 1) == ")") { | ||
var aName = key.substring(1, key.length - 1); | ||
var aInstance = new meta.Annotation(aName, ch.value()); | ||
aInstance.setOwnerFacet(appendedInfo); | ||
appendedInfo.addAnnotation(aInstance); | ||
} | ||
} | ||
} | ||
/** | ||
@@ -768,2 +780,3 @@ * parses a type from a JSON structure | ||
} | ||
var typePropAnnotations:tsInterfaces.IAnnotation[][] = []; | ||
if (!tp||ignoreTypeAttr){ | ||
@@ -788,2 +801,14 @@ if (defaultsToAny){ | ||
else{ | ||
var sAnnotations:ParseNode[][] = []; | ||
var actual = tp.childWithKey("value"); | ||
if(actual&&(actual.kind()==NodeKind.SCALAR||actual.kind()==NodeKind.ARRAY)){ | ||
sAnnotations = [ tp.children().filter(x=>{ | ||
var key = x.key(); | ||
if(!key){ | ||
return false; | ||
} | ||
return key.charAt(0) == "(" && key.charAt(key.length - 1) == ")"; | ||
}) ] | ||
tp = actual; | ||
} | ||
if (tp.kind()==NodeKind.SCALAR){ | ||
@@ -799,3 +824,19 @@ var valString = tp.value(); | ||
else if (tp.kind()==NodeKind.ARRAY){ | ||
superTypes=tp.children().map(x=>x.value()).map(y=>typeExpressions.parseToType(""+y,r, provider)); | ||
superTypes=tp.children().map(x=>{ | ||
var actual = x.childWithKey("value"); | ||
if(actual&&(actual.kind()==NodeKind.SCALAR||actual.kind()==NodeKind.ARRAY)){ | ||
sAnnotations.push(x.children().filter(x=>{ | ||
var key = x.key(); | ||
if(!key){ | ||
return false; | ||
} | ||
return key.charAt(0) == "(" && key.charAt(key.length - 1) == ")"; | ||
})); | ||
x = actual; | ||
} | ||
else{ | ||
sAnnotations.push([]); | ||
} | ||
return x.value(); | ||
}).map(y=>typeExpressions.parseToType(""+y,r, provider)); | ||
} | ||
@@ -805,4 +846,20 @@ else if (tp.kind()==NodeKind.MAP){ | ||
} | ||
if(sAnnotations.length>0 && sAnnotations.filter(x=>x.length>0).length>0) { | ||
for(var aArr of sAnnotations){ | ||
var aiArr:meta.Annotation[] = []; | ||
typePropAnnotations.push(aiArr); | ||
for(var ann of aArr) { | ||
var key = ann.key(); | ||
var aName = key.substring(1, key.length - 1); | ||
var aInstance = new meta.Annotation(aName, ann.value()); | ||
aiArr.push(aInstance); | ||
} | ||
} | ||
} | ||
} | ||
var result=ts.derive(name,superTypes); | ||
for(var i = 0 ; i < typePropAnnotations.length ; i++){ | ||
var aArr1:tsInterfaces.IAnnotation[] = typePropAnnotations[i]; | ||
result.addSupertypeAnnotation(aArr1,i); | ||
} | ||
if (r instanceof AccumulatingRegistry){ | ||
@@ -814,4 +871,10 @@ result = contributeToAccumulatingRegistry(result, r); | ||
n.children().forEach(x=>{ | ||
var key = x.key(); | ||
n.children().forEach(childNode=>{ | ||
var key = childNode.key(); | ||
actual = childNode.childWithKey("value"); | ||
var x = childNode; | ||
if(key!="example"&&actual){ | ||
x = actual; | ||
} | ||
if (!key){ | ||
@@ -837,39 +900,48 @@ return; | ||
} | ||
var appendedInfo:ts.TypeInformation; | ||
if (key=="items"){ | ||
if (result.isSubTypeOf(ts.ARRAY)){ | ||
var tp=parse(null, x,r,false,false,false); | ||
actualResult.addMeta(new ComponentShouldBeOfType(tp)); | ||
appendedInfo = new ComponentShouldBeOfType(tp); | ||
actualResult.addMeta(appendedInfo); | ||
actualResult.putExtra(tsInterfaces.HAS_ITEMS,true) | ||
if(actual){ | ||
appendAnnotations(appendedInfo, childNode); | ||
} | ||
return appendedInfo; | ||
} | ||
} | ||
else { | ||
if (key === "facets") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
return; | ||
} | ||
} | ||
else if (key == "default" || key == "xml" || key == "required") { | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals = key; | ||
} | ||
else if (key.charAt(0) == '(' && key.charAt(key.length - 1) == ')') { | ||
result.addMeta(new meta.Annotation(key.substr(1, key.length - 2), x.value())); | ||
return; | ||
} | ||
appendedInfo = facetR.getInstance().buildFacet(key, x.value()); | ||
if (key==="facets"){ | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals=key; | ||
return; | ||
} | ||
if (key=="default"||key=="xml"||key=="required"){ | ||
hasfacetsOrOtherStuffDoesNotAllowedInExternals=key; | ||
} | ||
if (key.charAt(0)=='('&& key.charAt(key.length-1)==')'){ | ||
result.addMeta(new meta.Annotation(key.substr(1, key.length-2), x.value())); | ||
return; | ||
} | ||
var vl=facetR.getInstance().buildFacet(key, x.value()); | ||
//TODO remove "format" condition and use this check for all facets | ||
if (vl && (key != "format" || testFacetAgainstType(vl, result))){ | ||
vl.setNode(x); | ||
result.addMeta(vl); | ||
} | ||
else{ | ||
if (annotation&&key==="allowedTargets"){ | ||
result.addMeta(new meta.AllowedTargets(x.value())); | ||
//TODO remove "format" condition and use this check for all facets | ||
if (appendedInfo && (key != "format" || testFacetAgainstType(appendedInfo, result))) { | ||
appendedInfo.setNode(x); | ||
result.addMeta(appendedInfo); | ||
} | ||
else { | ||
var customFacet = new meta.CustomFacet(key, x.value()); | ||
customFacet.setNode(x); | ||
result.addMeta(customFacet); | ||
if (annotation && key === "allowedTargets") { | ||
result.addMeta(new meta.AllowedTargets(x.value())); | ||
} | ||
else { | ||
var customFacet = new meta.CustomFacet(key, x.value()); | ||
customFacet.setNode(x); | ||
result.addMeta(customFacet); | ||
} | ||
} | ||
} | ||
if(appendedInfo){ | ||
appendAnnotations(appendedInfo, childNode); | ||
} | ||
}); | ||
@@ -898,2 +970,6 @@ if(result.metaOfType(meta.DiscriminatorValue).length==0){ | ||
if (ap){ | ||
actual = ap.childWithKey("value"); | ||
if(actual){ | ||
ap = actual; | ||
} | ||
result.addMeta(new KnownPropertyRestriction(ap.value())); | ||
@@ -900,0 +976,0 @@ } |
@@ -60,3 +60,3 @@ /// <reference path="../typings/main.d.ts" /> | ||
this,{typeName:t.name()}); | ||
s.setValidationPath(this.patchPath(q)); | ||
ts.setValidationPath(s,this.patchPath(q)); | ||
return s; | ||
@@ -66,3 +66,3 @@ } | ||
st.getErrors().forEach(x=>s.addSubStatus(x)); | ||
s.setValidationPath(this.patchPath(q)); | ||
ts.setValidationPath(s,this.patchPath(q)); | ||
return s; | ||
@@ -79,3 +79,3 @@ } | ||
var p=ts.error(messageRegistry.EXTERNAL_IN_PROPERTY_DEFINITION, this); | ||
p.setValidationPath({name: this.propId()}) | ||
ts.setValidationPath(p,{name: this.propId()}) | ||
return p; | ||
@@ -89,3 +89,3 @@ } | ||
}); | ||
p.setValidationPath({name: this.propId(), child: { name: "type"}}) | ||
ts.setValidationPath(p,{name: this.propId(), child: { name: "type"}}) | ||
return p; | ||
@@ -102,3 +102,3 @@ } | ||
p.setValidationPath({name: this.propId()}); | ||
ts.setValidationPath(p,{name: this.propId()}); | ||
return p; | ||
@@ -116,3 +116,3 @@ } | ||
}); | ||
p.setValidationPath({name: this.propId()}) | ||
ts.setValidationPath(p,{name: this.propId()}) | ||
return p; | ||
@@ -227,3 +227,3 @@ } | ||
var err=ts.error(messageRegistry.UNKNOWN_PROPERTY,this,{propName:x}); | ||
err.setValidationPath({name:x}); | ||
ts.setValidationPath(err,{name:x}); | ||
s.addSubStatus(err)} | ||
@@ -634,2 +634,3 @@ ); | ||
var superStatus = super.validateSelf(registry); | ||
var ownerIsCorrect = false; | ||
@@ -648,2 +649,3 @@ if (this.checkOwner(this.requiredType())) { | ||
var rs:ts.Status; | ||
if (!ownerIsCorrect){ | ||
@@ -661,11 +663,24 @@ | ||
rs.setValidationPath({name:this.facetName()}); | ||
return rs; | ||
} | ||
var rs=this.checkValue(); | ||
if (rs){ | ||
rs.setValidationPath({name:this.facetName()}); | ||
else { | ||
rs = this.checkValue(); | ||
} | ||
if (rs&&!rs.isOk()) { | ||
ts.setValidationPath(rs,{name: this.facetName()}); | ||
return rs; | ||
} | ||
return ts.ok(); | ||
var statuses = [superStatus,rs].filter(x=>x&&!x.isOk()); | ||
if(statuses.length==0) { | ||
return ts.ok(); | ||
} | ||
else if(statuses.length==1){ | ||
return statuses[0]; | ||
} | ||
else{ | ||
var result = ts.ok(); | ||
for(var status of statuses){ | ||
result.addSubStatus(status); | ||
} | ||
return result; | ||
} | ||
} | ||
@@ -1042,2 +1057,5 @@ | ||
checkValue():ts.Status{ | ||
if(typeof(this._value) != "boolean" ){ | ||
return ts.error(messageRegistry.UNIQUE_ITEMS_BOOLEAN,this); | ||
} | ||
return null; | ||
@@ -1078,3 +1096,3 @@ } | ||
} | ||
ss.setValidationPath({ name:""+j}); | ||
ts.setValidationPath(ss,{ name:""+j}); | ||
rs.addSubStatus(ss); | ||
@@ -1086,7 +1104,8 @@ } | ||
validateSelf(registry:ts.TypeRegistry):ts.Status { | ||
var st = super.validateSelf(registry); | ||
if (this.type.isAnonymous()) { | ||
var st = this.type.validateType(registry); | ||
if (!st.isOk()) { | ||
return ts.error(messageRegistry.INVALID_COMPONENT_TYPE, | ||
this,{msg: st.getMessage()}); | ||
var typeStatus = this.type.validateType(registry); | ||
if (!typeStatus.isOk()) { | ||
st.addSubStatus(ts.error(messageRegistry.INVALID_COMPONENT_TYPE, | ||
this,{msg: st.getMessage()})); | ||
} | ||
@@ -1096,14 +1115,15 @@ return st; | ||
if (this.type.isExternal()){ | ||
return ts.error(messageRegistry.EXTERNAL_AS_COMPONENT,this); | ||
st.addSubStatus(ts.error(messageRegistry.EXTERNAL_AS_COMPONENT,this)); | ||
} | ||
if (this.type.isSubTypeOf(ts.UNKNOWN) || this.type.isSubTypeOf(ts.RECURRENT)) { | ||
return ts.error(messageRegistry.UNKNOWN_AS_COMPONENT,this,{ typeName: this.type.name()}); | ||
else if (this.type.isSubTypeOf(ts.UNKNOWN) || this.type.isSubTypeOf(ts.RECURRENT)) { | ||
st.addSubStatus(ts.error(messageRegistry.UNKNOWN_AS_COMPONENT,this,{ typeName: this.type.name()})); | ||
} | ||
if (this.type.isUnion()) { | ||
else if (this.type.isUnion()) { | ||
var ui = _.find(this.type.typeFamily(), x=>x.isSubTypeOf(ts.UNKNOWN)); | ||
if (ui) { | ||
return ts.error(messageRegistry.UNKNOWN_AS_COMPONENT,this,{ typeName: ui.name()}); | ||
st.addSubStatus( | ||
ts.error(messageRegistry.UNKNOWN_AS_COMPONENT,this,{ typeName: ui.name()})); | ||
} | ||
} | ||
return ts.ok(); | ||
return st; | ||
} | ||
@@ -1110,0 +1130,0 @@ composeWith(t:ts.Constraint):ts.Constraint{ |
export interface IValidationPath{ | ||
name: string | ||
name: string|number | ||
child?:IValidationPath | ||
@@ -25,3 +25,3 @@ } | ||
/** | ||
* retur2ns true if status does not have errors | ||
* returns true if status does not have errors | ||
*/ | ||
@@ -39,2 +39,6 @@ isOk():boolean | ||
/** | ||
* return true if this status is just information | ||
*/ | ||
isInfo():boolean | ||
/** | ||
* returns human readable message associated with this status | ||
@@ -44,2 +48,4 @@ */ | ||
setMessage(m:string):void; | ||
/** | ||
@@ -62,2 +68,4 @@ * returns an array of nested statuses | ||
setValidationPath(p:IValidationPath):void; | ||
/** | ||
@@ -72,2 +80,6 @@ * returns path to this status | ||
getCode():string | ||
setCode(c:string):void; | ||
getSeverity(): number; | ||
} | ||
@@ -137,4 +149,25 @@ | ||
kind() : MetaInformationKind | ||
/** | ||
* Annotations applied to the facet | ||
*/ | ||
annotations():IAnnotation[] | ||
} | ||
/** | ||
* Model of annotation instances applied to types or their facets | ||
*/ | ||
export interface IAnnotation extends ITypeFacet { | ||
/** | ||
* Returns owner facet for annotations applied to facets | ||
*/ | ||
ownerFacet():ITypeFacet | ||
/** | ||
* Returns owner type for annotations applied to types | ||
*/ | ||
owner():IParsedType | ||
} | ||
export interface IParsedTypeCollection { | ||
@@ -325,2 +358,133 @@ | ||
isRecurrent():boolean; | ||
} | ||
} | ||
/** | ||
* A model of custom type validation plugin | ||
*/ | ||
export interface ITypeValidationPlugin { | ||
/** | ||
* @param t the type to be validated | ||
* @param reg context type registry | ||
*/ | ||
process(t:IParsedType, reg:ITypeRegistry):PluginValidationIssue[]; | ||
/** | ||
* String ID of the plugin | ||
*/ | ||
id():string; | ||
} | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
export function getTypeValidationPlugins():ITypeValidationPlugin[]{ | ||
var rv:any = (<any>global).ramlValidation; | ||
if(rv) { | ||
var typeValidators = rv.typeValidators; | ||
if (Array.isArray(typeValidators)) { | ||
return <ITypeValidationPlugin[]>typeValidators; | ||
} | ||
} | ||
return []; | ||
} | ||
/** | ||
* Model of annotation instance used as input fo validation plugins | ||
*/ | ||
export interface IAnnotationInstance{ | ||
/** | ||
* Annotation name | ||
*/ | ||
name():string; | ||
/** | ||
* Annotation value | ||
*/ | ||
value():any; | ||
/** | ||
* Annotation definition type | ||
*/ | ||
definition():IParsedType; | ||
} | ||
export interface PluginValidationIssue{ | ||
issueCode?:string, | ||
message?:string, | ||
isWarning?:boolean | ||
path?:IValidationPath | ||
} | ||
/** | ||
* Model of annotation validator for typesystem | ||
*/ | ||
export interface IAnnotationValidationPlugin { | ||
/** | ||
* validate annotated RAML element | ||
*/ | ||
process(entry:IAnnotatedElement):PluginValidationIssue[]; | ||
/** | ||
* String ID of the plugin | ||
*/ | ||
id():string; | ||
} | ||
/** | ||
* A model of annotated RAML element used as input for | ||
* annotation validation plugins | ||
*/ | ||
export interface IAnnotatedElement { | ||
/** | ||
* Element kind | ||
*/ | ||
kind():string; | ||
/** | ||
* Map view on the annotations applied | ||
*/ | ||
annotationsMap(): {[key:string]:IAnnotationInstance}; | ||
/** | ||
* Array view on the annotations applied | ||
*/ | ||
annotations(): IAnnotationInstance[]; | ||
/** | ||
* JSON representation of the entry | ||
*/ | ||
value(): any; | ||
/** | ||
* Element name | ||
*/ | ||
name(): string; | ||
/** | ||
* The element itself | ||
*/ | ||
entry():any; | ||
} | ||
/** | ||
* Retrieve a list of registered type validation plugins | ||
*/ | ||
export function getAnnotationValidationPlugins():IAnnotationValidationPlugin[]{ | ||
var rv:any = (<any>global).ramlValidation; | ||
if(rv) { | ||
var typesystemAnnotationValidators = rv.typesystemAnnotationValidators; | ||
if (Array.isArray(typesystemAnnotationValidators)) { | ||
return <IAnnotationValidationPlugin[]>typesystemAnnotationValidators; | ||
} | ||
} | ||
return []; | ||
} |
@@ -494,3 +494,3 @@ import ps= require("./actualParse") | ||
var st=t.validateType(ts.builtInRegistry()); | ||
assert.isTrue(st.getErrors().length===0); | ||
assert.isTrue(st.getErrors().length===2); | ||
}); | ||
@@ -497,0 +497,0 @@ it("properties is map", function () { |
@@ -6,2 +6,3 @@ { | ||
"mocha": "github:DefinitelyTyped/DefinitelyTyped/mocha/mocha.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd", | ||
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#3b2ed809b9e8f7dc4fcc1d80199129a0b73fb277", | ||
"typescript": "github:DefinitelyTyped/DefinitelyTyped/typescript/typescript.d.ts#3b2ed809b9e8f7dc4fcc1d80199129a0b73fb277", | ||
@@ -8,0 +9,0 @@ "underscore": "github:DefinitelyTyped/DefinitelyTyped/underscore/underscore.d.ts#01ce3ccf7f071514ff5057ef32a4550bf0b81dfe", |
@@ -42,4 +42,5 @@ { | ||
"gitUrl" : "https://github.com/mulesoft-labs/ts-model.git", | ||
"gitBranch": "develop", | ||
"installTypings" : true | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2237381
49488