Skip to content

Commit 6cf10a0

Browse files
committed
update validateSchema
1 parent 3850c91 commit 6cf10a0

File tree

1 file changed

+39
-104
lines changed

1 file changed

+39
-104
lines changed

src/main.go

Lines changed: 39 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,12 @@ func checkStatusCmd() tea.Msg {
12081208
if err != nil {
12091209
return statusMsg{text: strings.Join([]string{
12101210
fmt.Sprintf("Error reading schema: %v", err),
1211+
func() string {
1212+
if strings.Contains(err.Error(), "invalid character '}'") {
1213+
return "Double check you have no trailing commas."
1214+
}
1215+
return ""
1216+
}(),
12111217
"Please make sure a basic config file exists and is valid",
12121218
"you can also run 'basic init' to create a new project or import an existing project",
12131219
}, "\n")}
@@ -1289,7 +1295,7 @@ func checkStatusCmd() tea.Msg {
12891295
if valid.Valid != nil && !*valid.Valid {
12901296
messages = append(messages, "Errors found in schema! Please fix:")
12911297
for _, err := range valid.Errors {
1292-
messages = append(messages, fmt.Sprintf(" - %s", err.Message))
1298+
messages = append(messages, fmt.Sprintf(" - %s at %s", err.Message, err.InstancePath))
12931299
}
12941300
return statusMsg{text: strings.Join(messages, "\n"), status: "invalid", schema: schema, projectID: projectID}
12951301
}
@@ -1612,141 +1618,70 @@ func pushProjectSchema(schema string) (bool, error) {
16121618
return true, nil
16131619
}
16141620

1615-
func validateSchema(schema string) (struct {
1621+
// Add this type definition near the top of the file with other type definitions
1622+
type SchemaValidationResponse struct {
16161623
Valid *bool `json:"valid,omitempty"`
16171624
Errors []struct {
1618-
Message string `json:"message"`
1619-
Path string `json:"path"`
1620-
Change struct {
1621-
Path string `json:"path"`
1622-
} `json:"change"`
1625+
Message string `json:"message"`
1626+
InstancePath string `json:"instancePath"`
1627+
Params struct {
1628+
Field string `json:"field"`
1629+
OriginTable string `json:"originTable"`
1630+
ProjectID string `json:"project_id"`
1631+
} `json:"params"`
16231632
} `json:"errors,omitempty"`
16241633
Error *string `json:"error,omitempty"`
16251634
Message *string `json:"message,omitempty"`
1626-
}, error) {
1627-
// Create request body
1635+
}
1636+
1637+
// Then modify the validateSchema function to use this type
1638+
func validateSchema(schema string) (SchemaValidationResponse, error) {
1639+
// Parse schema string into JSON object first
1640+
var schemaObj map[string]interface{}
1641+
if err := json.Unmarshal([]byte(schema), &schemaObj); err != nil {
1642+
return SchemaValidationResponse{}, fmt.Errorf("error parsing schema JSON: %v", err)
1643+
}
1644+
1645+
// Create request body with JSON object
16281646
reqBody := struct {
1629-
Schema string `json:"schema"`
1647+
Schema map[string]interface{} `json:"schema"`
16301648
}{
1631-
Schema: schema,
1649+
Schema: schemaObj,
16321650
}
16331651

16341652
jsonBody, err := json.Marshal(reqBody)
16351653
if err != nil {
1636-
return struct {
1637-
Valid *bool `json:"valid,omitempty"`
1638-
Errors []struct {
1639-
Message string `json:"message"`
1640-
Path string `json:"path"`
1641-
Change struct {
1642-
Path string `json:"path"`
1643-
} `json:"change"`
1644-
} `json:"errors,omitempty"`
1645-
Error *string `json:"error,omitempty"`
1646-
Message *string `json:"message,omitempty"`
1647-
}{}, fmt.Errorf("error marshaling request body: %v", err)
1648-
}
1649-
1650-
// Make request to validation endpoint
1651-
resp, err := http.Post("https://api.basic.tech/schema/verifyUpdateSchema", "application/json", bytes.NewBuffer(jsonBody))
1654+
return SchemaValidationResponse{}, fmt.Errorf("error marshaling request body: %v", err)
1655+
}
1656+
1657+
resp, err := http.Post("http://localhost:3000/schema/verifyUpdateSchema", "application/json", bytes.NewBuffer(jsonBody))
16521658
if err != nil {
1653-
return struct {
1654-
Valid *bool `json:"valid,omitempty"`
1655-
Errors []struct {
1656-
Message string `json:"message"`
1657-
Path string `json:"path"`
1658-
Change struct {
1659-
Path string `json:"path"`
1660-
} `json:"change"`
1661-
} `json:"errors,omitempty"`
1662-
Error *string `json:"error,omitempty"`
1663-
Message *string `json:"message,omitempty"`
1664-
}{}, fmt.Errorf("error making validation request: %v", err)
1659+
return SchemaValidationResponse{}, fmt.Errorf("error making validation request: %v", err)
16651660
}
16661661
defer resp.Body.Close()
16671662

16681663
if resp.StatusCode != http.StatusOK {
1669-
// Read error response body first
16701664
bodyBytes, err := io.ReadAll(resp.Body)
16711665
if err != nil {
1672-
return struct {
1673-
Valid *bool `json:"valid,omitempty"`
1674-
Errors []struct {
1675-
Message string `json:"message"`
1676-
Path string `json:"path"`
1677-
Change struct {
1678-
Path string `json:"path"`
1679-
} `json:"change"`
1680-
} `json:"errors,omitempty"`
1681-
Error *string `json:"error,omitempty"`
1682-
Message *string `json:"message,omitempty"`
1683-
}{}, fmt.Errorf("error reading error response: %v", err)
1666+
return SchemaValidationResponse{}, fmt.Errorf("error reading error response: %v", err)
16841667
}
1685-
1686-
// Reset body for later use
16871668
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
16881669

16891670
var errResp struct {
16901671
Error string `json:"error"`
16911672
}
16921673
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
1693-
return struct {
1694-
Valid *bool `json:"valid,omitempty"`
1695-
Errors []struct {
1696-
Message string `json:"message"`
1697-
Path string `json:"path"`
1698-
Change struct {
1699-
Path string `json:"path"`
1700-
} `json:"change"`
1701-
} `json:"errors,omitempty"`
1702-
Error *string `json:"error,omitempty"`
1703-
Message *string `json:"message,omitempty"`
1704-
}{}, fmt.Errorf("error decoding error response: %v", err)
1674+
return SchemaValidationResponse{}, fmt.Errorf("error decoding error response: %v", err)
17051675
}
1706-
return struct {
1707-
Valid *bool `json:"valid,omitempty"`
1708-
Errors []struct {
1709-
Message string `json:"message"`
1710-
Path string `json:"path"`
1711-
Change struct {
1712-
Path string `json:"path"`
1713-
} `json:"change"`
1714-
} `json:"errors,omitempty"`
1715-
Error *string `json:"error,omitempty"`
1716-
Message *string `json:"message,omitempty"`
1717-
}{}, fmt.Errorf("error: %s", errResp.Error)
1718-
}
1719-
1720-
var response struct {
1721-
Valid *bool `json:"valid,omitempty"`
1722-
Errors []struct {
1723-
Message string `json:"message"`
1724-
Path string `json:"path"`
1725-
Change struct {
1726-
Path string `json:"path"`
1727-
} `json:"change"`
1728-
} `json:"errors,omitempty"`
1729-
Error *string `json:"error,omitempty"`
1730-
Message *string `json:"message,omitempty"`
1676+
return SchemaValidationResponse{}, fmt.Errorf("error: %s", errResp.Error)
17311677
}
17321678

1679+
var response SchemaValidationResponse
17331680
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
1734-
return struct {
1735-
Valid *bool `json:"valid,omitempty"`
1736-
Errors []struct {
1737-
Message string `json:"message"`
1738-
Path string `json:"path"`
1739-
Change struct {
1740-
Path string `json:"path"`
1741-
} `json:"change"`
1742-
} `json:"errors,omitempty"`
1743-
Error *string `json:"error,omitempty"`
1744-
Message *string `json:"message,omitempty"`
1745-
}{}, fmt.Errorf("error parsing validation response: %v", err)
1681+
return SchemaValidationResponse{}, fmt.Errorf("error parsing validation response: %v", err)
17461682
}
17471683

17481684
return response, nil
1749-
17501685
}
17511686

17521687
func getProjects(token *oauth2.Token) ([]project, error) {

0 commit comments

Comments
 (0)