-
Notifications
You must be signed in to change notification settings - Fork 55
Docs for data model fields calculation by expression #2763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| title: Calculation of data fields using expressions | ||
| description: How to configure calculation using dynamic expressions | ||
| toc: true | ||
| --- | ||
|
|
||
| Calculation of data fields using expressions makes it possible | ||
| to set data model fields with the expression engine through a JSON | ||
| schema spec. | ||
|
|
||
| ## How to configure calculation with expressions | ||
|
|
||
| {{% notice info %}} | ||
| Calculation with expressions cannot be configured through Altinn Studio Designer at present. | ||
| {{% /notice %}} | ||
|
|
||
| Calculation with expressions is defined in a separate file next to your data model, using the naming convention `name.calculation.json`. | ||
| If your data model is called `skjema`, you will already have files like `skjema.cs` and `skjema.schema.json`, and the file you create should be in the same folder and called `skjema.calculation.json`. | ||
| You can copy the content below as a starting point: | ||
|
|
||
| {{< code-title >}} | ||
| template.calculation.json | ||
| {{< /code-title >}} | ||
| ```json | ||
| { | ||
| "$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json", | ||
| "calculations": {} | ||
| } | ||
| ``` | ||
|
|
||
| ### Define a calculation rule | ||
|
|
||
| Below you can see an example of a calculation of the field `regnskap.sum` in the data model: | ||
|
|
||
| {{< code-title >}} | ||
| example.calculation.json | ||
| {{< /code-title >}} | ||
| ```json | ||
| { | ||
| "$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json", | ||
| "calculations": { | ||
| "regnskap.sum": [ | ||
| { | ||
| "condition": | ||
| ["minus", | ||
| ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"] | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The rules for the fields in the data model are set in the `calculations` object, where the data model path is the key and the value is a list of rules. | ||
|
|
||
| Unlike validation using expressions, calculations using expressions do not support lists. | ||
|
|
||
| A rule consists of a **condition**, which is a dynamic expression that returns any object type. See [dynamic expressions](/altinn-studio/v8/reference/logic/expressions/) for more information. | ||
|
Comment on lines
+54
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This reads as if calculation expressions can return arbitrary object values, but the linked expressions docs describe a constrained set of supported value types. Please document the supported/expected return types here instead of “any object type”. 🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||
| --- | ||||||||||||||||||
| title: Kalkulering av datafelt ved hjelp av uttrykk | ||||||||||||||||||
| description: Hvordan konfigurere kalkulering ved hjelp av dynamiske uttrykk | ||||||||||||||||||
| toc: true | ||||||||||||||||||
| --- | ||||||||||||||||||
|
|
||||||||||||||||||
| Kalkulering av datafelt ved hjelp av uttrykk gjør det mulig | ||||||||||||||||||
| å sette datamodellfelt med uttryksmotoren gjennom en json | ||||||||||||||||||
| schema spec. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Hvordan konfigurere kalkulering med uttrykk | ||||||||||||||||||
|
|
||||||||||||||||||
| {{% notice info %}} | ||||||||||||||||||
| Kalkulering med uttrykk kan ikke konfigureres igjennom Altinn Studio Designer per nå. | ||||||||||||||||||
| {{% /notice %}} | ||||||||||||||||||
|
|
||||||||||||||||||
| Kalkulering med uttryk defineres i en egen fil ved siden av datamodellen din, og bruker navne-konvensjonen `navn.calculation.json`. | ||||||||||||||||||
| Hvis datamodellen din heter `skjema` skal du blant annet ha filene `skjema.cs` og `skjema.schema.json` fra før, og da skal filen du oppretter ligge i samme mappe, og hete `skjema.calculation.json`. | ||||||||||||||||||
|
Comment on lines
+7
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Polish the Bokmål in the intro before publishing. This section still has a few visible language issues ( As per coding guidelines, 🤖 Prompt for AI Agents |
||||||||||||||||||
| Du kan kopiere innholdet nedenfor som et utgangspunkt: | ||||||||||||||||||
|
|
||||||||||||||||||
| {{< code-title >}} | ||||||||||||||||||
| template.calculation.json | ||||||||||||||||||
| {{< /code-title >}} | ||||||||||||||||||
| ```json | ||||||||||||||||||
| { | ||||||||||||||||||
| "$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json", | ||||||||||||||||||
| "calculations": {} | ||||||||||||||||||
| } | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Definere en kalkuleringsregel | ||||||||||||||||||
|
|
||||||||||||||||||
| Nedenfor kan du se et eksempel på en kalkulering av feltet `regnskap.sum` i datamodellen: | ||||||||||||||||||
|
|
||||||||||||||||||
| {{< code-title >}} | ||||||||||||||||||
| example.calculation.json | ||||||||||||||||||
| {{< /code-title >}} | ||||||||||||||||||
| ```json | ||||||||||||||||||
| { | ||||||||||||||||||
| "$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/calculation/calculation.schema.v1.json", | ||||||||||||||||||
| "calculations": { | ||||||||||||||||||
| "regnskap.sum": [ | ||||||||||||||||||
| { | ||||||||||||||||||
| "condition": | ||||||||||||||||||
| ["minus", | ||||||||||||||||||
| ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"] | ||||||||||||||||||
| ] | ||||||||||||||||||
|
Comment on lines
+44
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The example uses the wrong expression function name. Line 46 uses Suggested fix "condition":
["minus",
- ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
+ ["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"]
]📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| } | ||||||||||||||||||
| ] | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Reglene for feltene i datamodellen settes i `calculations`-objektet, hvor datamodell-stien er nøkkelen, og verdien er en liste med regler. | ||||||||||||||||||
|
|
||||||||||||||||||
| I motsetning til validering ved hjelp av uttrykk, støtter ikke kalkuleringer ved hjelp av uttrykk lister. | ||||||||||||||||||
|
|
||||||||||||||||||
| En regel består av en **condition**, som er et dynamisk uttrykk som returnerer en hvilke som helst objekttype. Se [dynamiske uttrykk](/nb/altinn-studio/v8/reference/logic/expressions/) for mer informasjon. | ||||||||||||||||||
|
Comment on lines
+54
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please narrow the return-type description here.
🤖 Prompt for AI Agents |
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1078,7 +1078,7 @@ gir dette uttrykket en feilmelding. | |
|
|
||
| ## Datatyper | ||
|
|
||
| Funksjoner i uttrykkene har en forventning om at argumentene som blir sendt inn har en spefikk type. Dersom et argument | ||
| Funksjoner i uttrykkene har en forventning om at argumentene som blir sendt inn har en spesifikk type. Dersom et argument | ||
| blir sendt inn har en annen type enn forventet, blir verdien forsøkt konvertert til riktig type. Som et eksempel | ||
|
Comment on lines
+1081
to
1082
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the sentence grammar here. The typo is fixed, but the sentence is still ungrammatical: As per coding guidelines, 🤖 Prompt for AI Agents |
||
| forventer funksjonen `equals` to strenger, men om du sender inn den boolske verdien `true` som det ene eller andre | ||
| argumentet fungerer det også fint, siden den boolske verdien `true` blir konvertert til strengen `"true"`. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example should use
dataModelconsistently.The first lookup uses
datamodel, while the second usesdataModel. IfdataModelis the actual function name, this example will fail when copied as written.Suggested fix
"condition": ["minus", - ["datamodel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"] + ["dataModel", "regnskap.inntekter"], ["dataModel", "regnskap.utgifter"] ]📝 Committable suggestion
🤖 Prompt for AI Agents