Semantic models

Wrapper types that address a specific use-case.

unique

available since: 1.0.0

Unique is a marker model that can wrap any submodel. In that case, karma.run will prevent the creation of object sharing the same value at that position.

Example:

Query:
e.data(m.unique(m.struct({
  • foo: m.string(),
  • bar: m.string(),
}
)
)
)
Result:
{ "unique": { "struct": { "bar": { "string": {} }, "foo": { "string": {} } } } }
Notes:

A string model with unique content

Example:

Query:
{
  • query: e.createMultiple(e.tag(d.string("_user")), {
    • a: f.function(["refs"], e.data(d.struct({
      • username: d.string("test-user"),
      • password: d.string("$2a$04$0grY4cHFfy330bCokWYDJ.9CvTG5gqEeOGlNnbGxZDkVYThCC8eOS"),
      • roles: d.list(d.expr(e.refTo(e.first(e.all(e.tag(d.string("_role"))))))),
      }
      )
      )
      )
      ,
    • b: f.function(["refs"], e.data(d.struct({
      • username: d.string("test-user"),
      • password: d.string("$2a$04$0grY4cHFfy330bCokWYDJ.9CvTG5gqEeOGlNnbGxZDkVYThCC8eOS"),
      • roles: d.list(d.expr(e.refTo(e.first(e.all(e.tag(d.string("_role"))))))),
      }
      )
      )
      )
      ,
    }
    )
    ,
  • result: ,
}
Notes:

This query based on the model definition of _user which has a unique username would fail because we try to create two times the a user with the same name.

annotation

available since: 1.0.0

Annotations make it easy to associate third-party information with a model. karma.run ignores these annotations but persists them with the model for external services to read.

Example:

Query:
e.data(m.struct({
  • annotatedKey: m.annotation("custom annotation string", m.string()),
}
)
)
Result:
{ "struct": { "annotatedKey": { "annotation": { "model": { "string": {} }, "value": "custom annotation string" } } } }
Notes:

In the example above, we're binding the string ui:slider(-103,205) to a model of type string.

optional

available since: 1.0.0

Optional is a marker model that can wrap any submodel. In that case, karma.run will accept null values from external type systems (such as JSON's) at that position. In other words, it adds null to the set of acceptable values at that position in a model.

Example:

Query:
e.data(m.struct({
  • myString: m.string(),
  • myOptionalString: m.optional(m.string()),
}
)
)
Result:
{ "struct": { "myOptionalString": { "optional": { "string": {} } }, "myString": { "string": {} } } }
Notes:

A struct model composed of two keys, where the key 'myOptionalString' is optional

Example:

Query:
e.data(d.struct({
  • myString: d.string("foo"),
  • myOptionalString: d.string("bar"),
}
)
)
Result:
{ "myOptionalString": "bar", "myString": "foo" }
Notes:

A struct content based on the model definition above.

Example:

Query:
e.data(d.struct({
  • myString: d.string("foo"),
}
)
)
Result:
{ "myString": "foo" }
Notes:

We can omit the key 'myOptionalString' because he is optional

numeric

available since: 1.0.0

Numeric is not a distinct type. Numeric just refers to one of the following primitive numeric types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, float