$schema: "https://json-schema.org/draft/2020-12/schema"
$id: "arc-workspace-project.schema.yml"
title: ARC Workspace Project
description: Structural schema for .arc/project.yml
type: object

properties:
  type:
    const: ArcWorkspaceProject
  workspaceProfiles:
    type: array
    items:
      $ref: "#/$defs/profileReference"
  rules:
    type: array
    items:
      $ref: "#/$defs/storageRule"

required:
  - type

anyOf:
  - properties:
      workspaceProfiles:
        minItems: 1
    required:
      - workspaceProfiles
  - properties:
      rules:
        minItems: 1
    required:
      - rules

additionalProperties: false

$defs:
  identifier:
    type: string
    pattern: "^[A-Za-z][A-Za-z0-9._-]*$"
    maxLength: 128

  version:
    type: string
    pattern: "^[A-Za-z0-9][A-Za-z0-9._+-]*$"
    maxLength: 64

  datasetIdentifier:
    type: string
    minLength: 1

  safeRelativePath:
    type: string
    minLength: 1
    pattern: '^(?!/)(?![A-Za-z][A-Za-z0-9+.-]*:)(?!.*//)(?!.*\\)(?!.*(?:^|/)\.{1,2}(?:/|$))(?!.*\u0000).+$'

  pathTemplate:
    allOf:
      - $ref: "#/$defs/safeRelativePath"
      - pattern: '^(?:[^/{}]+|\{dataset\.identifier\})(?:/(?:[^/{}]+|\{dataset\.identifier\}))*$'

  auxiliaryPath:
    allOf:
      - $ref: "#/$defs/safeRelativePath"
      - pattern: '^[^/{}]+(?:/[^/{}]+)*$'

  profileReference:
    type: object
    properties:
      file:
        $ref: "#/$defs/safeRelativePath"
      url:
        type: string
        minLength: 1
        format: uri
        pattern: "^https?://[^#]+$"
    oneOf:
      - required:
          - file
        not:
          required:
            - url
      - required:
          - url
        not:
          required:
            - file
    additionalProperties: false

  target:
    oneOf:
      - const: root
      - type: object
        properties:
          identifier:
            $ref: "#/$defs/datasetIdentifier"
        required:
          - identifier
        additionalProperties: false
      - type: object
        properties:
          additionalType:
            type: string
            minLength: 1
        required:
          - additionalType
        additionalProperties: false

  storageRule:
    type: object
    properties:
      id:
        $ref: "#/$defs/identifier"
      codec:
        $ref: "#/$defs/identifier"
      target:
        $ref: "#/$defs/target"
      path:
        $ref: "#/$defs/pathTemplate"
      files:
        type: array
        minItems: 1
        items:
          $ref: "#/$defs/storageFile"
    required:
      - id
      - codec
      - target
      - path
    additionalProperties: false

  storageFile:
    type: object
    properties:
      id:
        $ref: "#/$defs/identifier"
      path:
        $ref: "#/$defs/auxiliaryPath"
      create:
        const: empty
    required:
      - id
      - path
    additionalProperties: false
