@@ -84,6 +84,7 @@ func init() {
8484 convertSchemaCMD .Flags ().String ("output" , "pretty" , "output pretty or raw. use raw for machine-readable output or writing to a file" )
8585 schemaCmd .AddCommand (convertSchemaCMD )
8686 schemaCmd .AddCommand (getSchemaCmd )
87+ schemaCmd .AddCommand (validateSchemaCmd )
8788 applySchemaCmd .Flags ().BoolP ("verbose" , "v" , false , "print extra details about the request" )
8889 applySchemaCmd .Flags ().Bool ("strict" , false , "fail if there are warnings" )
8990 schemaCmd .AddCommand (applySchemaCmd )
@@ -689,10 +690,10 @@ var convertSchemaCMD = &cobra.Command{
689690 printer .PrintMsg ("Version:" )
690691 printer .PrintMsg (fmt .Sprintf ("%s\n " , response .Version ))
691692
692- if response .Warnings != nil {
693- printer .PrintMsg ("Warnings:" )
693+ if len ( response .Warnings ) > 0 {
694+ printer .PrintMsg (printer . YellowText ( "Warnings:" ) )
694695 for _ , warning := range response .Warnings {
695- printer .PrintMsg (warning .Message )
696+ printer .PrintMsg (printer . YellowText ( warning .Message ) )
696697 }
697698 printer .PrintMsg ("\n " )
698699 }
@@ -750,6 +751,38 @@ var getSchemaCmd = &cobra.Command{
750751 },
751752}
752753
754+ var validateSchemaCmd = & cobra.Command {
755+ Use : "validate <input_file>" ,
756+ Short : "Validate a schema file for issues" ,
757+ Long : "Validate a schema file, printing any warnings or errors found. Only schema files are accepted (not resource type JSON)." ,
758+ Example : `workos fga schema validate schema.txt` ,
759+ Args : cobra .ExactArgs (1 ),
760+ RunE : func (cmd * cobra.Command , args []string ) error {
761+ bytes , err := os .ReadFile (args [0 ])
762+ if err != nil {
763+ return errors .Errorf ("error reading input file: %v" , err )
764+ }
765+
766+ response , err := fga .ConvertSchemaToResourceTypes (context .Background (), fga.ConvertSchemaToResourceTypesOpts {
767+ Schema : string (bytes ),
768+ })
769+ if err != nil {
770+ return convertSchemaError (err )
771+ }
772+
773+ if len (response .Warnings ) > 0 {
774+ printer .PrintMsg (printer .YellowText ("Warnings:" ))
775+ for _ , warning := range response .Warnings {
776+ printer .PrintMsg (printer .YellowText ("* " + warning .Message ))
777+ }
778+ return nil
779+ }
780+
781+ printer .PrintMsg (printer .GreenText (printer .Checkmark + " No issues" ))
782+ return nil
783+ },
784+ }
785+
753786var applySchemaCmd = & cobra.Command {
754787 Use : "apply <input_file>" ,
755788 Short : "Apply a schema" ,
@@ -778,10 +811,10 @@ var applySchemaCmd = &cobra.Command{
778811 return convertSchemaError (err )
779812 }
780813
781- if response .Warnings != nil {
782- printer .PrintMsg ("Warnings:" )
814+ if len ( response .Warnings ) > 0 {
815+ printer .PrintMsg (printer . YellowText ( "Warnings:" ) )
783816 for _ , warning := range response .Warnings {
784- printer .PrintMsg (warning .Message )
817+ printer .PrintMsg (printer . YellowText ( "* " + warning .Message ) )
785818 }
786819 printer .PrintMsg ("\n " )
787820 if strict {
@@ -1228,11 +1261,15 @@ Each test file must have a 'setup' section, a 'tests' array, and an optional 'te
12281261func convertSchemaError (err error ) error {
12291262 var target workos_errors.HTTPError
12301263 if errors .As (err , & target ) {
1264+ formattedErrors := make ([]string , 0 , len (target .Errors ))
1265+ for _ , e := range target .Errors {
1266+ formattedErrors = append (formattedErrors , printer .RedText ("\t * " + e ))
1267+ }
12311268 if len (target .Errors ) > 0 {
1232- return errors .Errorf ( "error converting schema: %s\n \t %s" , target .Message , strings .Join (target . Errors , "\n \t " ))
1269+ return errors .New ( printer . RedText ( fmt . Sprintf ( "error validating schema: %s\n %s" , target .Message , strings .Join (formattedErrors , "\n " )) ))
12331270 }
12341271 }
1235- return errors .Errorf ( "error converting schema: %v" , err )
1272+ return errors .New ( printer . RedText ( fmt . Sprintf ( "error validating schema: %v" , err )) )
12361273}
12371274
12381275func warrantCheckAsString (w fga.WarrantCheck ) (string , error ) {
0 commit comments