diff --git a/schemas/discriminated.json b/schemas/discriminated.json new file mode 100644 index 00000000..29649659 --- /dev/null +++ b/schemas/discriminated.json @@ -0,0 +1,159 @@ +{ + "$id": "@next", + "$schema": "http://json-schema.org/draft-07/schema#", + "anyOf": [ + { + "$ref": "#/definitions/PlanningPermissionApplication" + }, + { + "$ref": "#/definitions/PriorApprovalApplication" + }, + { + "$ref": "#/definitions/WorksToTreesApplication" + } + ], + "definitions": { + "PPUser": { + "additionalProperties": false, + "properties": { + "ppSpecificProperty": { + "type": "boolean" + }, + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + } + }, + "required": [ + "ppSpecificProperty", + "role" + ], + "type": "object" + }, + "PlanningPermissionApplication": { + "additionalProperties": false, + "description": "Application for a \"Planning Permission\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/PPUser" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "pp", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + }, + "PriorApprovalApplication": { + "additionalProperties": false, + "description": "Application for a \"Prior Approval\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/UserBase" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "pa", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + }, + "UserBase": { + "additionalProperties": false, + "properties": { + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + } + }, + "required": [ + "role" + ], + "type": "object" + }, + "WTTUser": { + "additionalProperties": false, + "properties": { + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + }, + "wttSpecificProperty": { + "type": "boolean" + } + }, + "required": [ + "role", + "wttSpecificProperty" + ], + "type": "object" + }, + "WorksToTreesApplication": { + "additionalProperties": false, + "description": "Application for a \"Works to trees\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/WTTUser" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "wtt", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + } + }, + "description": "The root specification for a planning application in England generated by a digital planning service", + "title": "App" +} \ No newline at end of file diff --git a/scripts/build-schema.sh b/scripts/build-schema.sh index 7eee9132..0b7dd37a 100755 --- a/scripts/build-schema.sh +++ b/scripts/build-schema.sh @@ -7,8 +7,8 @@ version="${VERSION:-@next}" echo "Version set to $version" -dirs=("digitalPlanningApplication") -types=("DigitalPlanningApplication") +dirs=("digitalPlanningApplication" "discriminated") +types=("DigitalPlanningApplication" "App") for i in "${!dirs[@]}"; do dir=${dirs[$i]} diff --git a/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts b/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts index b9d516de..40bc0d7c 100644 --- a/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts +++ b/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts @@ -180,6 +180,10 @@ type GenericApplicationType = { description: (typeof ApplicationTypes)[TKey]; }; +type ExtractPrimaryKeys = T extends `${infer K}.${string}` ? K : T; + +export type PrimaryApplicationTypes = ExtractPrimaryKeys; + type ApplicationTypeMap = { [K in ApplicationTypeKeys]: GenericApplicationType; }; diff --git a/types/schemas/discriminated/User.ts b/types/schemas/discriminated/User.ts new file mode 100644 index 00000000..09afce99 --- /dev/null +++ b/types/schemas/discriminated/User.ts @@ -0,0 +1,30 @@ +import {PrimaryApplicationTypes} from '../digitalPlanningApplication/enums/ApplicationTypes'; + +export interface UserBase { + role: 'applicant' | 'agent' | 'proxy'; +} + +export interface WTTUser extends UserBase { + wttSpecificProperty: boolean; +} + +export interface PPUser extends UserBase { + ppSpecificProperty: boolean; +} + +/** + * @internal + */ +interface UserVariants { + wtt: WTTUser; + pp: PPUser; +} + +/** + * @internal + */ +export type User = + T extends keyof UserVariants + ? UserVariants[T] + : // Default value for non-specific application types + UserBase; diff --git a/types/schemas/discriminated/index.ts b/types/schemas/discriminated/index.ts new file mode 100644 index 00000000..580f77b9 --- /dev/null +++ b/types/schemas/discriminated/index.ts @@ -0,0 +1,33 @@ +import {PrimaryApplicationTypes} from '../digitalPlanningApplication/enums/ApplicationTypes'; +import {User} from './User'; + +interface GenericApplication { + primaryApplicationType: T; + data: { + user: User; + }; +} + +/** + * @description Application for a "Planning Permission" project + */ +export type PlanningPermissionApplication = GenericApplication<'pp'>; + +/** + * @description Application for a "Prior Approval" project + */ +export type PriorApprovalApplication = GenericApplication<'pa'>; + +/** + * @description Application for a "Works to trees" project + */ +export type WorksToTreesApplication = GenericApplication<'wtt'>; + +/** + * @title App + * @description The root specification for a planning application in England generated by a digital planning service + */ +export type App = + | PlanningPermissionApplication + | PriorApprovalApplication + | WorksToTreesApplication;