TypeError: datastore.getRootCollection is not a function - dojo

I assumed a dstore that was created using dmodel was still a dstore and inherited all the dstore functionality. So there's a method listed called getRootCollection but when i try to run this method on the store it fails with an error (no such function)
Here is my code
<script>
require(
[
'dojo/_base/declare',
'dstore/Memory',
'dmodel/extensions/jsonSchema',
'dmodel/validators/StringValidator',
'dmodel/store/Validating',
"dmodel/Model"
],
function (declare, Memory, jsonSchema, StringValidator, Validating, Model) {
var vMem = (declare([Memory, Validating]))({
Model: jsonSchema(
{
"$schema": "http://json-schema.org/draft-04/schema",
"description": "my schema",
"type": "object",
"properties": {
"page": {
"type": "object",
"properties": {
"detailsCanvas": {
"description": "test value",
"type": "object",
"$ref": "#/definitions/details"
}
}
},
"elements": {
"type": "array",
"items": {
"title": "Element",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"positionX": {
"description": "The X coordinate",
"type": "number"
},
"elementSpecificProperties": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/label" }
]
}
}
}
}
},
"definitions": {
"details": {
"type": "object",
"properties": {
"height": {
"type": "string"
}
}
},
"label": {
"type": "object",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
)
});
vMem.setData(
{
"page": {
"detailsCanvas": {
"height": "100px"
}
},
"elements": [
{
"id": "1",
"positionX": 20,
"elementSpecificProperties": {
"value": "value_1"
}
},
{
"id": "2",
"positionX": 5,
"elementSpecificProperties": {
"value": "value_2"
}
}
]
});
var blah = vMem.getRootCollection(); //type error - getRootCollection is not a function
});
</script>

getRootCollection() is part of the Tree model of dstore. You only mixin the dstore/Memory. So that is why you get that error. Checkout the documentation of dstore for more information.

Related

Easy way to group custom defined types to use for reference?

How to reference types by group instead of doing it individually?
For example here, I want "sum" and "subtract" types to accept types that are "number"-related types only("number", "sum", "subtract"). While "concatenate" can accept both "number" and "string"-related types. Including their selves. This will be really helpful for me if in case I added more types and want them to be automatically included.
"sum": {
"title": "sum",
"type": "object",
"properties": {
"values": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/definitions/sum"
},
{
"$ref": "#/definitions/subtract"
}
]
}
}
},
"required": [
"values"
]
},
"subtract": {
"title": "subtract",
"type": "object",
"properties": {
"minuend": {
"type": "number"
},
"subtrahend": {
"type": "number"
}
},
"required": [
"minuend",
"subtrahend"
]
},
"concatenate": {
"title": "concatenate",
"type": "object",
"properties": {
"strings": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"$ref": "#/definitions/sum"
},
{
"$ref": "#/definitions/subtract"
}
]
}
}
},
"required": [
"strings"
]
}
}
Here's the full schema:
{
"definitions": {
"sum": {
"title": "sum",
"type": "object",
"properties": {
"values": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/definitions/sum"
},
{
"$ref": "#/definitions/subtract"
}
]
}
}
},
"required": [
"values"
]
},
"subtract": {
"title": "subtract",
"type": "object",
"properties": {
"minuend": {
"type": "number"
},
"subtrahend": {
"type": "number"
}
},
"required": [
"minuend",
"subtrahend"
]
},
"concatenate": {
"title": "concatenate",
"type": "object",
"properties": {
"strings": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"$ref": "#/definitions/sum"
},
{
"$ref": "#/definitions/subtract"
}
]
}
}
},
"required": [
"strings"
]
}
},
"title": "Step",
"type": "object",
"properties": {
"value": {
"oneOf": [
{
"type": "number",
"minimum": 0
},
{
"$ref": "#/definitions/sum"
},
{
"$ref": "#/definitions/subtract"
}
]
}
}
}

Generated OpenAPI golang client doesn't seem to handle dates correctly?

I'm working on an API that provides a list of transactions. I'm writing it in Java/Kotlin Spring, but prefer golang for CLIs, so I'm generating a golang client for it. The API works well in the Swagger UI.
Kotlin API:
#GetMapping
fun listTransactions() : ResponseEntity<List<Transaction>> {
val transactions = ArrayList<Transaction>()
transactionRepository.findAll().mapTo(transactions) { fromEntity(it) }
return ResponseEntity.ok(transactions)
}
Kotlin Object:
data class Transaction(
val id: Long,
val transactionDate: Date, // Java SQL date
val postedDate: Date?, // Java SQL date
val amount: BigDecimal,
val category: Category,
val merchant: Merchant,
val merchantDescription: String?
)
Generated Schema:
{
"openapi": "3.0.1",
"info": {
"title": "BFI Swagger Title",
"description": "BFI description",
"version": "0.1"
},
"servers": [{
"url": "http://localhost:8080",
"description": "Generated server url"
}],
"paths": {
"/transaction": {
"get": {
"tags": ["transaction-router"],
"operationId": "listTransactions",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Transaction"
}
}
}
}
}
}
},
"post": {
"tags": ["transaction-router"],
"operationId": "createTransaction",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateTransactionRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Transaction"
}
}
}
}
}
}
},
"/hello": {
"get": {
"tags": ["category-router"],
"summary": "Hello there!",
"operationId": "hello",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Category"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"CreateTransactionRequest": {
"type": "object",
"properties": {
"transactionDate": {
"type": "string",
"format": "date-time"
},
"postedDate": {
"type": "string",
"format": "date-time"
},
"amount": {
"type": "number"
},
"categoryId": {
"type": "integer",
"format": "int64"
},
"merchantId": {
"type": "integer",
"format": "int64"
},
"merchantDescription": {
"type": "string"
}
}
},
"Category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"note": {
"type": "string"
}
}
},
"Merchant": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"note": {
"type": "string"
}
}
},
"Transaction": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"transactionDate": {
"type": "string",
"format": "date-time"
},
"postedDate": {
"type": "string",
"format": "date-time"
},
"amount": {
"type": "number"
},
"category": {
"$ref": "#/components/schemas/Category"
},
"merchant": {
"$ref": "#/components/schemas/Merchant"
},
"merchantDescription": {
"type": "string"
}
}
}
}
}
}
Golang Client Object:
type Transaction struct {
Id *int64 `json:"id,omitempty"`
TransactionDate *time.Time `json:"transactionDate,omitempty"`
PostedDate *time.Time `json:"postedDate,omitempty"`
Amount *float32 `json:"amount,omitempty"`
Category *Category `json:"category,omitempty"`
Merchant *Merchant `json:"merchant,omitempty"`
MerchantDescription *string `json:"merchantDescription,omitempty"`
}
All of this seems correct enough. However, when I use the OpenAPI client, it seems like deserialization isn't working correctly:
Error when calling `TransactionRouterApi.ListTransactions``: parsing time "\"2022-10-28\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "\"" as "T"
Full HTTP response: &{200 200 HTTP/1.1 1 1 map[Content-Type:[application/json] Date:[Sat, 29 Oct 2022 04:03:31 GMT]] {[{"id":1,"transactionDate":"2022-10-28","postedDate":"2022-10-28","amount":0.00,"category":{"id":1,"name":"test","note":"test"},"merchant":{"id":1,"name":"test","note":"test"},"merchantDescription":null},{"id":2,"transactionDate":"2022-10-28","postedDate":"2022-10-28","amount":0.00,"category":{"id":1,"name":"test","note":"test"},"merchant":{"id":1,"name":"test","note":"test"},"merchantDescription":null},{"id":3,"transactionDate":"2022-10-28","postedDate":"2022-10-28","amount":0.00,"category":{"id":1,"name":"test","note":"test"},"merchant":{"id":1,"name":"test","note":"test"},"merchantDescription":null}]} -1 [chunked] false false map[] 0x140001daf00 <nil>}
Response from `TransactionRouterApi.ListTransactions`: [{0x140000aa218 0001-01-01 00:00:00 +0000 UTC <nil> <nil> <nil> <nil> <nil>}]
Am I doing something incorrectly that results in deserialization failing? Or is this a client bug (seems doubtful, but who knows).
I've looked at the generation arguments I used and the schema available at my endpoint, and they both appear correct.
Script executed: openapi-generator-cli generate -g go -i http://localhost:8080/v3/api-docs
Two options:
Create a custom type and implement the Unmarshaler interface for the date fields.
Return valid RFC 3339 date/times from the API.

JSON Schema - How to narrow down possible array object values

How can the following requirements be configured in a JSON schema.
I have the following JSON
{
"orderMethods": [
{
"requestType": "some service",
"deployType": "MANUAL",
"deployLabel": "some label"
},
{
"requestType": "some service",
"deployType": "MANUAL",
"deployLabel": "some label"
},
{
"requestType": "some service",
"deployType": "AUTO",
"deploySubType": "REST",
"deployLabel": "some label",
"deployConfig": "some config"
},
{
"requestType": "some service",
"deployType": "AUTO",
"deploySubType": "DB",
"deployLabel": "some label",
"deployConfig": "some config"
}
]
}
Requirements:
only one deployType = "MANUAL" is allowed
only one deployType = "AUTO" is allowed
additionally, only one deploySubType = "REST or "DB" is allowed
Currently I have the following JSON schema. Unfortunately the above JSON is valid. How can the above requirements be addressed?
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"orderMethods": {
"$ref": "#/definitions/OrderMethods"
}
},
"additionalProperties": false,
"definitions": {
"OrderMethods": {
"type": "array",
"items": {
"$ref": "#/definitions/OrderMethod"
}
},
"OrderMethod": {
"oneOf": [
{
"$ref": "#/definitions/OrderMethodManualDeploy"
},
{
"$ref": "#/definitions/OrderMethodAutoDeploy"
}
],
"uniqueItems": true
},
"OrderMethodAutoDeploy": {
"oneOf": [
{
"$ref": "#/definitions/OrderMethodAutoRestDeploy"
},
{
"$ref": "#/definitions/OrderMethodAutoDBDeploy"
}
],
"uniqueItems": true
},
"OrderMethodManualDeploy": {
"type": "object",
"properties": {
"requestType": {
"type": "string"
},
"deployType": {
"const": "MANUAL"
},
"deployLabel": {
"type": "string"
}
},
"required": [
"requestType",
"deployType",
"deployLabel"
],
"additionalProperties": false
},
"OrderMethodAutoRestDeploy": {
"type": "object",
"properties": {
"requestType": {
"type": "string"
},
"deployType": {
"const": "AUTO"
},
"deploySubType": {
"const": "REST"
},
"deployLabel": {
"type": "string"
},
"deployConfig": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"requestType",
"deployType",
"deploySubType",
"deployLabel",
"deployConfig"
]
},
"OrderMethodAutoDBDeploy": {
"type": "object",
"properties": {
"requestType": {
"type": "string"
},
"deployType": {
"const": "AUTO"
},
"deploySubType": {
"const": "DB"
},
"deployConfig": {
"type": "string"
},
"deployLabel": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"requestType",
"deployType",
"deploySubType",
"deployLabel",
"deployConfig"
]
}
}
}
Thanks for your help/ideas.

JSON schema definition with multiple sets of enums for array attribute

I want to create a JSON schema for an object in which one of the attributes is restricted to multiple sets of enums.
For example:
{
"data": {
"type": "myObject",
"attributes": {
"states": [
"Washington",
"Oregon",
"California"
]
}
}
}
is a valid JSON object against the schema.
And
{
"data": {
"type": "myObject",
"attributes": {
"states": [
"British Columbia",
"Alberta",
"Ontario"
]
}
}
}
is also a valid JSON object agains the schema
BUT,
{
"data": {
"type": "myObject",
"attributes": {
"states": [
"Washington",
"Oregon",
"Alberta"
]
}
}
}
is NOT a valid JSON object against the schema.
I have tried the following schema definition:
{
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"attributes": {
"type": "object",
"properties": {
"states": {
"type": "array",
"items": {
"oneOf": [
{
"enum": ["Washington","Oregon","California"],
"description": "United States"
},
{
"enum": ["British Columbia","Alberta", "Ontario"],
"description": "Canada"
}
]
},
"description": "Filter by states"
}
}
}
}
}
}
}
But with this schema above this is still considered valid:
{
"data": {
"type": "myObject",
"attributes": {
"states": [
"Washington",
"Oregon",
"Alberta"
]
}
}
}
BTW, you can use this for testing whether a JSON object conforms to a schema: https://www.jsonschemavalidator.net/
Thank you!
You need to invert the order of the oneOf and the items keywords, so that the same oneOf clause is used for all items:
...
"states": {
"type": "array",
"oneOf": [
{
"items": {
"enum": ["Washington","Oregon","California"],
"description": "United States"
}
},
{
"items": {
"enum": ["British Columbia","Alberta", "Ontario"],
"description": "Canada"
}
}
]
},
...

JSONSchema v4 - Error when resolving schema reference - Definitions and References

When trying to validate the following schema using http://www.jsonschemavalidator.net/,
{
"id": "http://some.site.somewhere/entry-schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for the FormularSpecification",
"definitions": {
"elementId": {
"id": "http://jsonschema.net/elementId",
"type": "string"
},
"mappingKey": {
"id": "http://jsonschema.net/mappingKey",
"type": "string"
},
"elementType": {
"id": "http://jsonschema.net/elementType",
"type": "string"
},
"length": {
"id": "http://jsonschema.net/length",
"type": "integer"
},
"label": {
"id": "http://jsonschema.net/label",
"type": "string"
},
"content": {
"id": "http://jsonschema.net/content",
"type": "string"
},
"placeholder": {
"id": "http://jsonschema.net/placeholder",
"type": "string"
},
"date": {
"id": "http://jsonschema.net/date",
"type": "string"
},
"option": {
"id": "http://jsonschema.net/option",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"label": { "$ref": "#/definitions/label" }
},
"required": ["elementId", "label"]
},
"options": {
"id": "http://jsonschema.net/options",
"type": "array",
"items": { "$ref": "#/definitions/option" },
"minItems": 1,
"uniqueItems": true
},
"textfield": {
"id": "http://jsonschema.net/textfield",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"length": { "$ref": "#/definitions/length" },
"label": { "$ref": "#/definitions/label" },
"placeholder": { "$ref": "#/definitions/placeholder" },
"textfieldType": {
"enum": [ "text", "ext4", "btrfs" ]
}
},
"required": ["elementId", "length", "label", "placeholder", "textfieldType"]
},
"checkbox": {
"id": "http://jsonschema.net/checkbox",
"type": "object",
"properties": {
"label": { "$ref": "#/definitions/label" }
},
"required": ["label"]
},
"radio": {
"id": "http://jsonschema.net/radio",
"type": "object",
"properties": {
"label": { "$ref": "#/definitions/label" },
"options": { "$ref": "#/definitions/options" }
},
"required": ["label", "options"]
},
"dropdown": {
"id": "http://jsonschema.net/dropdown",
"type": "object",
"properties": {
"label": { "$ref": "#/definitions/label" },
"options": { "$ref": "#/definitions/options" }
},
"required": ["label", "options"]
},
"validator": {
"id": "http://jsonschema.net/validator",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" }
}
},
"validators": {
"id": "http://jsonschema.net/validators",
"type": "array",
"items": { "$ref": "#/definitions/validator" }
},
"interactiveDetails": {
"type": "object",
"oneOf": [
{ "textfield": { "$ref": "#/definitions/textfield" } },
{ "checkbox": { "$ref": "#/definitions/checkbox" } },
{ "radio": { "$ref": "#/definitions/radio" } },
{ "dropdown": { "$ref": "#/definitions/dropdown" } },
{ "date": { "$ref": "#/definitions/date" } }
]
},
"interactive": {
"id": "http://jsonschema.net/interactive",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"elementType": { "$ref": "#/definitions/elementType" },
"mappingKey": { "$ref": "#/definitions/mappingKey" },
"validators": { "$ref": "#/definitions/validators" },
"interactiveDetails" : { "$ref": "#/definitions/interactiveDetails" }
},
"required": ["elementId", "elementType", "mappingKey", "validators"]
},
"interactives": {
"id": "http://jsonschema.net/interactives",
"type": "array",
"items": { "$ref": "#/definitions/interactive" }
},
"description": {
"id": "http://jsonschema.net/description",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"elementType": { "$ref": "#/definitions/elementType" },
"content": { "$ref": "#/definitions/content" }
},
"required": ["elementId", "elementType", "content"]
},
"descriptions": {
"items": { "$ref": "#/definitions/description" }
},
"children": {
"items": {
"anyOf": [
{ "$ref": "#/definitions/group" },
{ "$ref": "#/definitions/question" }
]
},
"minItems": 1
},
"question": {
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"descriptions": { "$ref": "#/definitions/descriptions" },
"interactives": { "$ref": "#/definitions/interactives" }
},
"required": ["elementId", "descriptions", "interactives"]
},
"group": {
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"descriptions": { "$ref": "#/definitions/descriptions" },
"children": { "$ref": "#/definitions/children"}
},
"required": ["elementId", "descriptions", "children"]
}
},
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"description": { "$ref": "#/definitions/descriptions" },
"children": { "$ref": "#/definitions/children" }
},
"required": [
"elementId",
"descriptions",
"children"
]
}
I'm getting the following error:
Error when resolving schema reference '#/definitions/elementId'. Path 'definitions.description.properties.elementId', line 135, position 30.
I cannot figure out what the problem is. I scanned the documentation several times and had a look at tutorials, but I do not have any clue.
The semantics of the id keyword are are a bit confusing. I'm not sure I completely understand it myself. In general it is almost never a good idea to include id anywhere other than the root of your schema.
The "id" keyword (or "id", for short) is used to alter the resolution scope. When an id is encountered, an implementation MUST resolve this id against the most immediate parent scope. The resolved URI will be the new resolution scope for this subschema and all its children, until another id is encountered.
http://json-schema.org/latest/json-schema-core.html#anchor27
Consider the following excerpt from you schema. Because you include the id keyword, your "elementId" and "label" $refs don't resolve against the root of the document as you expect, they resolve from the nearest parent schema id.
"option": {
"id": "http://jsonschema.net/option",
"type": "object",
"properties": {
"elementId": { "$ref": "#/definitions/elementId" },
"label": { "$ref": "#/definitions/label" }
},
"required": ["elementId", "label"],
"definitions": { ... } <-- your $refs expect values here
}
},
I have seen that in some circles, people write schemas with ids for every subschema. I'm not sure what benefit they think they are getting out of doing that, but I suspect that they think of id as just a label and don't understand how it alters resolution scope.
If you do have good reason to use ids everywhere and want to leave them in, you can always explicitly reference the root id when you have a conflict.
{ "$ref": "http://some.site.somewhere/entry-schema#definitions/elementId" }