@defra/land-grants-rules-schemas 0.0.0-stage → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. package/dist/features/common/constants/action_availability.js +5 -0
  2. package/dist/features/common/constants/unit_type.js +26 -0
  3. package/dist/features/grants-config/schema/action-config.schema.js +74 -0
  4. package/dist/features/rules-engine/rules/1.0.0/applied-for-total-available-area/applied-for-total-available-area.schema.js +4 -0
  5. package/dist/features/rules-engine/rules/1.0.0/applied-for-total-or-partial-available-area/applied-for-total-or-partial-available-area.schema.js +4 -0
  6. package/dist/features/rules-engine/rules/1.0.0/applied-for-total-or-partial-available-length/applied-for-total-or-partial-available-length.schema.js +4 -0
  7. package/dist/features/rules-engine/rules/1.0.0/boundary-intersection-consent-required/boundary-intersection-consent-required.schema.js +8 -0
  8. package/dist/features/rules-engine/rules/1.0.0/hefer-consent-required/hefer-consent-required.schema.js +7 -0
  9. package/dist/features/rules-engine/rules/1.0.0/manual-check-required/manual-check-required.schema.js +3 -0
  10. package/dist/features/rules-engine/rules/1.0.0/min-max-parcel-size/min-max-parcel-size.schema.js +6 -0
  11. package/dist/features/rules-engine/rules/1.0.0/minimum-length/minimum-length.schema.js +3 -0
  12. package/dist/features/rules-engine/rules/1.0.0/parcel-has-intersection-with-data-layer/parcel-has-intersection-with-data-layer.schema.js +8 -0
  13. package/dist/features/rules-engine/rules/1.0.0/parcel-has-valid-land-cover/parcel-has-valid-land-cover.schema.js +3 -0
  14. package/dist/features/rules-engine/rules/1.0.0/parcel-intersection-does-not-exceed-maximum-for-data-layer/parcel-intersection-does-not-exceed-maximum-for-data-layer.schema.js +8 -0
  15. package/dist/features/rules-engine/rules/1.0.0/schemas.js +40 -0
  16. package/dist/features/rules-engine/rules/1.0.0/sssi-consent-required/sssi-consent-required.schema.js +7 -0
  17. package/dist/features/rules-engine/rules/1.0.0/woodland-minimum-eligibility/woodland-minimum-eligibility.schema.js +6 -0
  18. package/dist/features/rules-engine/rules/1.0.0/woodland-total-area/woodland-total-area.schema.js +4 -0
  19. package/dist/features/rules-engine/rules/rule-key.js +12 -0
  20. package/dist/features/rules-engine/rules/schemas.js +46 -0
  21. package/dist/index.js +1 -0
  22. package/package.json +24 -4
  23. package/README.md +0 -3
@@ -0,0 +1,5 @@
1
+ export const TOTAL = 'total'
2
+ const PARTIAL = 'partial'
3
+
4
+ // 'limited' removed for now — not yet a real scenario in the data; add back when needed.
5
+ export const AVAILABILITY_TYPES = [TOTAL, PARTIAL]
@@ -0,0 +1,26 @@
1
+ export const HECTARES = 'ha'
2
+ export const COUNT = 'count'
3
+ export const METERS = 'm'
4
+ export const SQM = 'sqm'
5
+
6
+ export const UNIT_TYPES = [HECTARES, SQM, METERS, COUNT]
7
+
8
+ // Unit which must be integers
9
+ export const INTEGER_UNITS = [SQM, METERS, COUNT]
10
+
11
+ // Land area reaches us as sqm on agreements and as ha on planned actions
12
+ const AREA_UNITS = new Set([HECTARES, SQM])
13
+
14
+ /**
15
+ * Whether a quantity in this unit competes for a parcel's land area
16
+ * @param {string|undefined} unit
17
+ * @returns {boolean}
18
+ */
19
+ export const isAreaUnit = (unit) => AREA_UNITS.has(unit ?? '')
20
+
21
+ /**
22
+ * Whether a quantity in this unit competes for a parcel's boundary
23
+ * @param {string|undefined} unit
24
+ * @returns {boolean}
25
+ */
26
+ export const isLengthUnit = (unit) => unit === METERS
@@ -0,0 +1,74 @@
1
+ import Joi from 'joi'
2
+ import { AVAILABILITY_TYPES } from '../../common/constants/action_availability.js'
3
+ import { UNIT_TYPES } from '../../common/constants/unit_type.js'
4
+ import { ruleSchema } from '../../rules-engine/rules/schemas.js'
5
+
6
+ // Relative imports only: the schemas package copies this file and its imports as they are
7
+
8
+ const paymentSchema = Joi.object({
9
+ ratePerAgreementPerYearGbp: Joi.number(),
10
+ ratePerUnitGbp: Joi.number()
11
+ })
12
+
13
+ const paymentTierSchema = Joi.object({
14
+ flatRateGbp: Joi.number(),
15
+ lowerLimitHa: Joi.number(),
16
+ ratePerUnitGbp: Joi.number(),
17
+ upperLimitHa: Joi.number().allow(null)
18
+ })
19
+
20
+ const paymentMethodConfigSchema = Joi.object({
21
+ newWoodlandMaxPercent: Joi.number(),
22
+ ratePerAgreementPerYearGbp: Joi.number(),
23
+ ratePerUnitGbp: Joi.number(),
24
+ tiers: Joi.array().items(paymentTierSchema)
25
+ }).unknown(true)
26
+
27
+ const paymentMethodSchema = Joi.object({
28
+ config: paymentMethodConfigSchema,
29
+ name: Joi.string().required(),
30
+ version: Joi.string()
31
+ })
32
+
33
+ const availabilitySchema = Joi.object({
34
+ type: Joi.string()
35
+ .valid(...AVAILABILITY_TYPES)
36
+ .required()
37
+ })
38
+
39
+ const actionConfigSchema = Joi.object({
40
+ applicationUnitOfMeasurement: Joi.string()
41
+ .valid(...UNIT_TYPES)
42
+ .required(),
43
+ availability: availabilitySchema,
44
+ code: Joi.string().required(),
45
+ description: Joi.string().required(),
46
+ display: Joi.boolean().required(),
47
+ displayOrder: Joi.number().required(),
48
+ displayUnit: Joi.string(),
49
+ displayUnitPlural: Joi.string(),
50
+ durationYears: Joi.number().required(),
51
+ enabled: Joi.boolean().required(),
52
+ groupId: Joi.number().integer().allow(null).required(),
53
+ guidanceUrl: Joi.string().uri(),
54
+ landCoverClassCodes: Joi.array().items(Joi.string()),
55
+ payment: paymentSchema.allow(null).required(),
56
+ paymentMethod: paymentMethodSchema.required(),
57
+ rotationType: Joi.string(),
58
+ rules: Joi.array().items(ruleSchema).required(),
59
+ semanticVersion: Joi.string().required(),
60
+ startDate: Joi.string().isoDate().required(),
61
+ isSupplementalOnly: Joi.boolean().optional()
62
+ })
63
+
64
+ /**
65
+ * Checks an action config against the schema, reporting every problem rather than the first
66
+ * @param {object} actionConfig - An action config as published by the grants config repo
67
+ * @returns {{ error?: Joi.ValidationError }}
68
+ */
69
+ export const validateActionConfig = (actionConfig) => {
70
+ const { error } = actionConfigSchema.validate(actionConfig, {
71
+ abortEarly: false
72
+ })
73
+ return { error }
74
+ }
@@ -0,0 +1,4 @@
1
+ import Joi from 'joi'
2
+
3
+ // This rule reads nothing from rule.config
4
+ export default Joi.forbidden()
@@ -0,0 +1,4 @@
1
+ import Joi from 'joi'
2
+
3
+ // This rule reads nothing from rule.config
4
+ export default Joi.forbidden()
@@ -0,0 +1,4 @@
1
+ import Joi from 'joi'
2
+
3
+ // This rule reads nothing from rule.config
4
+ export default Joi.forbidden()
@@ -0,0 +1,8 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ layerName: Joi.string().required(),
5
+ caveatCode: Joi.string().required(),
6
+ caveatDescription: Joi.string().required(),
7
+ toleranceMeters: Joi.number()
8
+ })
@@ -0,0 +1,7 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ layerName: Joi.string().required(),
5
+ caveatDescription: Joi.string().required(),
6
+ tolerancePercent: Joi.number().required()
7
+ })
@@ -0,0 +1,3 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({ caveatDescription: Joi.string().required() })
@@ -0,0 +1,6 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ minimumParcelSizeSqm: Joi.number(),
5
+ maximumParcelSizeSqm: Joi.number()
6
+ }).or('minimumParcelSizeSqm', 'maximumParcelSizeSqm')
@@ -0,0 +1,3 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({ minimumLengthM: Joi.number().required() })
@@ -0,0 +1,8 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ layerName: Joi.string().required(),
5
+ minimumIntersectionPercent: Joi.number().required(),
6
+ tolerancePercent: Joi.number().required(),
7
+ failureMessage: Joi.string()
8
+ })
@@ -0,0 +1,3 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({ failureMessage: Joi.string() }).optional()
@@ -0,0 +1,8 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ layerName: Joi.string().required(),
5
+ maximumIntersectionPercent: Joi.number().required(),
6
+ tolerancePercent: Joi.number(),
7
+ failureMessage: Joi.string()
8
+ })
@@ -0,0 +1,40 @@
1
+ import appliedForTotalAvailableAreaSchema from './applied-for-total-available-area/applied-for-total-available-area.schema.js'
2
+ import appliedForTotalOrPartialAvailableAreaSchema from './applied-for-total-or-partial-available-area/applied-for-total-or-partial-available-area.schema.js'
3
+ import appliedForTotalOrPartialAvailableLengthSchema from './applied-for-total-or-partial-available-length/applied-for-total-or-partial-available-length.schema.js'
4
+ import boundaryIntersectionConsentRequiredSchema from './boundary-intersection-consent-required/boundary-intersection-consent-required.schema.js'
5
+ import heferConsentRequiredSchema from './hefer-consent-required/hefer-consent-required.schema.js'
6
+ import manualCheckRequiredSchema from './manual-check-required/manual-check-required.schema.js'
7
+ import minMaxParcelSizeSchema from './min-max-parcel-size/min-max-parcel-size.schema.js'
8
+ import minimumLengthSchema from './minimum-length/minimum-length.schema.js'
9
+ import parcelHasIntersectionWithDataLayerSchema from './parcel-has-intersection-with-data-layer/parcel-has-intersection-with-data-layer.schema.js'
10
+ import parcelHasMinimumEligibilityForWoodlandManagementPlanSchema from './woodland-minimum-eligibility/woodland-minimum-eligibility.schema.js'
11
+ import parcelHasValidLandCoverSchema from './parcel-has-valid-land-cover/parcel-has-valid-land-cover.schema.js'
12
+ import parcelIntersectionDoesNotExceedMaximumForDataLayerSchema from './parcel-intersection-does-not-exceed-maximum-for-data-layer/parcel-intersection-does-not-exceed-maximum-for-data-layer.schema.js'
13
+ import sssiConsentRequiredSchema from './sssi-consent-required/sssi-consent-required.schema.js'
14
+ import totalAreaNotExceedLandParcelsWoodlandManagementPlanSchema from './woodland-total-area/woodland-total-area.schema.js'
15
+
16
+ // Keyed the same way as ./index.js. Rules that take no config use Joi.forbidden()
17
+ export const configSchemaByRuleKey = {
18
+ 'applied-for-total-available-area-1.0.0': appliedForTotalAvailableAreaSchema,
19
+ 'applied-for-total-or-partial-available-area-1.0.0':
20
+ appliedForTotalOrPartialAvailableAreaSchema,
21
+ 'applied-for-total-or-partial-available-length-1.0.0':
22
+ appliedForTotalOrPartialAvailableLengthSchema,
23
+ 'available-length-1.0.0': appliedForTotalOrPartialAvailableLengthSchema,
24
+ 'boundary-intersection-consent-required-1.0.0':
25
+ boundaryIntersectionConsentRequiredSchema,
26
+ 'hefer-consent-required-1.0.0': heferConsentRequiredSchema,
27
+ 'manual-check-required-1.0.0': manualCheckRequiredSchema,
28
+ 'min-max-parcel-size-1.0.0': minMaxParcelSizeSchema,
29
+ 'minimum-length-1.0.0': minimumLengthSchema,
30
+ 'parcel-has-intersection-with-data-layer-1.0.0':
31
+ parcelHasIntersectionWithDataLayerSchema,
32
+ 'parcel-has-minimum-eligibility-for-woodland-management-plan-1.0.0':
33
+ parcelHasMinimumEligibilityForWoodlandManagementPlanSchema,
34
+ 'parcel-has-valid-land-cover-1.0.0': parcelHasValidLandCoverSchema,
35
+ 'parcel-intersection-does-not-exceed-maximum-for-data-layer-1.0.0':
36
+ parcelIntersectionDoesNotExceedMaximumForDataLayerSchema,
37
+ 'sssi-consent-required-1.0.0': sssiConsentRequiredSchema,
38
+ 'total-area-not-exceed-land-parcels-woodland-management-plan-1.0.0':
39
+ totalAreaNotExceedLandParcelsWoodlandManagementPlanSchema
40
+ }
@@ -0,0 +1,7 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ layerName: Joi.string().required(),
5
+ caveatDescription: Joi.string().required(),
6
+ tolerancePercent: Joi.number().required()
7
+ })
@@ -0,0 +1,6 @@
1
+ import Joi from 'joi'
2
+
3
+ export default Joi.object({
4
+ minimumSize: Joi.number().required(),
5
+ minOldWoodlandHa: Joi.number().required()
6
+ })
@@ -0,0 +1,4 @@
1
+ import Joi from 'joi'
2
+
3
+ // This rule reads nothing from rule.config
4
+ export default Joi.forbidden()
@@ -0,0 +1,12 @@
1
+ const DEFAULT_RULE_VERSION = '1.0.0'
2
+
3
+ // `type` lets a config-defined rule name (e.g. a per-action caveat identity like
4
+ // 'pond-check-required') be dispatched to a shared, generic executor (e.g.
5
+ // 'manual-check-required') without needing its own registry entry. Falls back to
6
+ // `name` for every existing rule, which has no `type` - dispatch is unchanged for them.
7
+ /**
8
+ * @param {{ name: string, type?: string, version?: string }} rule
9
+ * @returns {string}
10
+ */
11
+ export const ruleKeyFor = (rule) =>
12
+ `${rule.type ?? rule.name}-${rule.version ?? DEFAULT_RULE_VERSION}`
@@ -0,0 +1,46 @@
1
+ import Joi from 'joi'
2
+ import { configSchemaByRuleKey as configSchemaByRuleKeyV1 } from './1.0.0/schemas.js'
3
+ import { ruleKeyFor } from './rule-key.js'
4
+
5
+ // Keyed the same way as ./index.js. Spread each new version folder in here.
6
+ export const configSchemaByRuleKey = { ...configSchemaByRuleKeyV1 }
7
+
8
+ // Config is required unless the schema opts out with .optional() or .forbidden()
9
+ const configFor = (schema) =>
10
+ schema.describe().flags?.presence ? schema : schema.required()
11
+
12
+ // The engine runs rules[ruleKeyFor(rule)], so this looks up configSchemaByRuleKey[ruleKeyFor(rule)]
13
+ const configSchema = Joi.when(
14
+ // '..' is the parent of config, the whole rule, which adjust turns into its rule key
15
+ Joi.ref('..', { adjust: ruleKeyFor }),
16
+ {
17
+ // One case per rule key; config is validated against the first that matches.
18
+ // A key with no case leaves config unchecked, which is why isKnownRule exists
19
+ switch: Object.entries(configSchemaByRuleKey).map(([key, schema]) => ({
20
+ is: key,
21
+ then: configFor(schema)
22
+ }))
23
+ }
24
+ )
25
+
26
+ const isKnownRule = (rule, helpers) =>
27
+ ruleKeyFor(rule) in configSchemaByRuleKey
28
+ ? rule
29
+ : helpers.message(
30
+ { custom: '"{{#ruleKey}}" is an unknown rule' },
31
+ { ruleKey: ruleKeyFor(rule) }
32
+ )
33
+
34
+ /**
35
+ * Schema for one entry of an action's `rules` array, from the rules engine's
36
+ * side only: the engine runs the rule keyed by `ruleKeyFor`, so that must be a
37
+ * rule it has, and `config` must satisfy that rule. Which rules an action is
38
+ * allowed to use is the action schema's job, not this package's.
39
+ */
40
+ export const ruleSchema = Joi.object({
41
+ description: Joi.string().required(),
42
+ name: Joi.string().required(),
43
+ type: Joi.string(),
44
+ version: Joi.string(),
45
+ config: configSchema
46
+ }).custom(isKnownRule)
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { validateActionConfig } from './features/grants-config/schema/action-config.schema.js'
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@defra/land-grants-rules-schemas",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.2.0",
4
+ "description": "Joi schemas for land-grants-api action config and its rules",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/DEFRA/land-grants-api.git",
9
+ "directory": "packages/rules-schemas"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "main": "dist/index.js",
15
+ "exports": "./dist/index.js",
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "prepack": "node scripts/build.js"
21
+ },
22
+ "dependencies": {
23
+ "joi": "^17.13.9"
24
+ },
25
+ "license": "OGL-UK-3.0"
26
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.