Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Annotations in nominals #101

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import schemaUtil = require('./schemaUtil');

export type IValidationPath = tsInterfaces.IValidationPath;
export type IHasExtra = tsInterfaces.IHasExtra;
export type IAnnotation = tsInterfaces.IAnnotation;
export var TOP_LEVEL_EXTRA = tsInterfaces.TOP_LEVEL_EXTRA;
export var DEFINED_IN_TYPES_EXTRA = tsInterfaces.DEFINED_IN_TYPES_EXTRA;
export var USER_DEFINED_EXTRA = tsInterfaces.USER_DEFINED_EXTRA;
Expand Down
19 changes: 18 additions & 1 deletion src/nominals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts=require("./typesystem")
import ti=require("./nominal-interfaces")
import nt=require("./nominal-types")
import parse=require("./parse")
import restrictions = require("./restrictions");
Expand Down Expand Up @@ -135,17 +136,33 @@ export function toNominal(t:ts.AbstractType,callback:StringToBuiltIn,customizer:

var proto=parse.toProto(t);
proto.properties.forEach(x=>{
var propName = x.regExp ? `/${x.id}/` : x.id;
var propName = x.regExp ? `/${x.id}/` : x.id;
var prop=pc ? pc(propName):new nt.Property(propName);
prop.withDomain(<nt.StructuredType>vs);
prop.withRange(toNominal(x.type,callback));
const descrMeta = x.type.metaOfType(metainfo.Description)
if (descrMeta && descrMeta.length > 0 && descrMeta[0].value() && descrMeta[0].value().length && descrMeta[0].value().length > 0) {
prop.withDescription(descrMeta[0].value())
}
const annMetas = x.type.metaOfType(<{ new(v:any):metainfo.Annotation }>metainfo.Annotation)
if (annMetas && annMetas.length && annMetas.length > 0) {
annMetas.forEach(ann => {
const nameId = () => ann.facetName()
const val = { value: ann.value() }
prop.addAnnotation(new nt.Annotation(<ti.IAnnotationType>{nameId: nameId }, val))
})
}
if (!x.optional){
prop.withRequired(true);
}
if(x.regExp){
prop.withKeyRegexp(propName);
}
});
proto.annotations.forEach(ann => {
const nameId = () => ann.facetName()
vs.addAnnotation(new nt.Annotation(<ti.IAnnotationType>{ nameId: nameId }, ann.value()))
});
proto.facetDeclarations.filter(x=>!x.isBuiltIn()).forEach(x=>{
var prop=pc?pc(x.facetName()):new nt.Property(x.facetName());
prop.withRange(toNominal(x.type(),callback));
Expand Down
51 changes: 51 additions & 0 deletions tests/toNominalTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,55 @@ describe("To nominals",function() {
var nt=nm.toNominal(tp,x=>null);
assert.isTrue(nt.superTypes().length==2);
});
it("to nominal properties inherit annotations", function () {
const tps = ps.parseJSONTypeCollection({
annotationTypes: {"(testAnnotation)" : {type: "string"}},
types: {
A: { type: "object", properties: {
x: { type: "string", "(testAnnotation)" : "testVal"}}}
}
},ts.builtInRegistry());
const tp=tps.getType("A");
const nt=nm.toNominal(tp,x=>null);
const prop = nt.property("x");

assert.isDefined(prop.annotations());
assert.equal(prop.annotations().length, 1);
assert.equal(prop.annotations()[0].nameId(), "testAnnotation");
assert.equal(prop.annotations()[0].parameterNames()[0], "value");
assert.equal(prop.annotations()[0].parameter("value"), "testVal");
});
it("to nominal types inherit annotations", function () {
const tps = ps.parseJSONTypeCollection({
annotationTypes: {"(testAnnotation)" : {
type: "object",
properties: {
propA: "string",
propB: "integer",
}
}},
types: {
A: {
type: "object",
"(testAnnotation)": {
propA : "string ann prop",
propB : 123
},
properties: {
x: "string"
}
}
}
},ts.builtInRegistry());
const tp=tps.getType("A");
const nt=nm.toNominal(tp,x=>null);

assert.isDefined(nt.annotations());
assert.equal(nt.annotations().length, 1);
assert.equal(nt.annotations()[0].nameId(), "testAnnotation");
assert.equal(nt.annotations()[0].parameterNames()[0], "propA");
assert.equal(nt.annotations()[0].parameterNames()[1], "propB");
assert.equal(nt.annotations()[0].parameter("propA"), "string ann prop");
assert.equal(nt.annotations()[0].parameter("propB"), 123);
});
});