github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/job/validate.go (about)

     1  package job
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  
     8  	"github.com/filecoin-project/bacalhau/pkg/model"
     9  )
    10  
    11  // VerifyJobCreatePayload verifies the values in a job creation request are legal.
    12  func VerifyJobCreatePayload(ctx context.Context, jc *model.JobCreatePayload) error {
    13  	if jc.ClientID == "" {
    14  		return fmt.Errorf("ClientID is empty")
    15  	}
    16  
    17  	if jc.APIVersion == "" {
    18  		return fmt.Errorf("APIVersion is empty")
    19  	}
    20  
    21  	return VerifyJob(ctx, &model.Job{
    22  		APIVersion: jc.APIVersion,
    23  		Spec:       *jc.Spec,
    24  	})
    25  }
    26  
    27  // VerifyJob verifies that job object passed is valid.
    28  func VerifyJob(ctx context.Context, j *model.Job) error {
    29  	if reflect.DeepEqual(model.Spec{}, j.Spec) {
    30  		return fmt.Errorf("job spec is empty")
    31  	}
    32  
    33  	if reflect.DeepEqual(model.Deal{}, j.Spec.Deal) {
    34  		return fmt.Errorf("job deal is empty")
    35  	}
    36  
    37  	if j.Spec.Deal.Concurrency <= 0 {
    38  		return fmt.Errorf("concurrency must be >= 1")
    39  	}
    40  
    41  	if j.Spec.Deal.Confidence < 0 {
    42  		return fmt.Errorf("confidence must be >= 0")
    43  	}
    44  
    45  	if !model.IsValidEngine(j.Spec.Engine) {
    46  		return fmt.Errorf("invalid executor type: %s", j.Spec.Engine.String())
    47  	}
    48  
    49  	if !model.IsValidVerifier(j.Spec.Verifier) {
    50  		return fmt.Errorf("invalid verifier type: %s", j.Spec.Verifier.String())
    51  	}
    52  
    53  	if !model.IsValidPublisher(j.Spec.Publisher) {
    54  		return fmt.Errorf("invalid publisher type: %s", j.Spec.Publisher.String())
    55  	}
    56  
    57  	if err := j.Spec.Network.IsValid(); err != nil {
    58  		return err
    59  	}
    60  
    61  	if j.Spec.Deal.Confidence > j.Spec.Deal.Concurrency {
    62  		return fmt.Errorf("the deal confidence cannot be higher than the concurrency")
    63  	}
    64  
    65  	for _, inputVolume := range j.Spec.Inputs {
    66  		if !model.IsValidStorageSourceType(inputVolume.StorageSource) {
    67  			return fmt.Errorf("invalid input volume type: %s", inputVolume.StorageSource.String())
    68  		}
    69  	}
    70  
    71  	return nil
    72  }