github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/common/schema.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/xeipuuv/gojsonschema"
     9  )
    10  
    11  const LatestYamlVersion = 20180708
    12  
    13  const V20180708Schema = `{
    14      "title": "V20180708 func file schema",
    15      "type": "object",
    16      "properties": {
    17          "name": {
    18              "type":"string"
    19          },
    20          "schema_version": {
    21              "type":"integer"
    22          },
    23          "version": {
    24              "type":"string"
    25          },
    26          "runtime": {
    27              "type":"string"
    28          },
    29          "build_image": {
    30              "type":"string"
    31          },
    32          "run_image": {
    33              "type": "string"
    34          },
    35          "entrypoint": {
    36              "type":"string"
    37          },
    38          "content_type": {
    39              "type":"string"
    40          },
    41          "cmd": {
    42              "type":"string"
    43          },
    44          "memory": {
    45              "type":"integer"
    46          },
    47          "timeout": {
    48              "type":"integer"
    49          },
    50          "idle_timeout": {
    51              "type": "integer"
    52          },
    53          "config": {
    54              "type": "object"
    55          },
    56          "triggers": {
    57              "type": "array",
    58              "properties": {
    59                  "name": {
    60                      "type":"string"
    61                  },
    62                  "type": {
    63                      "type":"string"
    64                  },
    65                  "source": {
    66                      "type":"string"
    67                  }
    68              }
    69          }
    70      }
    71  }`
    72  
    73  func ValidateFileAgainstSchema(jsonFile, schema string) error {
    74  	schemaLoader := gojsonschema.NewStringLoader(schema)
    75  	documentLoader := gojsonschema.NewReferenceLoader(filepath.Join("file://", GetWd(), jsonFile))
    76  
    77  	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if !result.Valid() {
    83  		fmt.Println("The func.yaml is not valid. Please see errors: ")
    84  		for _, desc := range result.Errors() {
    85  			fmt.Printf("- %s\n", desc)
    86  		}
    87  		return errors.New("Please update your func.yaml to satisfy the schema.")
    88  	}
    89  
    90  	return nil
    91  }