Skip to content

Commit

Permalink
feat: add rule configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lekotros committed May 4, 2022
1 parent 717e246 commit 80d9563
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
14 changes: 12 additions & 2 deletions app/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ class ApiClient {
});
}

async validate(id: string, schema: string) {
async validate(id: string, schema: string, rules: string[]) {
return axios({
method: 'get',
url: this.withUrl(`sessions/${id}/validate`),
params: { schema },
params: { schema, rules },
paramsSerializer: params => {
return Object.keys(params).map((k) => {
const v = params[k];

return (Array.isArray(v) ? v : [v])
.map(v => `${k}=${v}`)
.join('&');
})
.join('&');
},
})
.then(res => res.data);
}
Expand Down
45 changes: 37 additions & 8 deletions app/components/ValidationConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ const StatusChip = ({ status }: StatusChipProps) => {

type ValidationConfigProps = {
session: Session;
onValidate: (schema: string) => void;
onValidate: (schema: string, rules: string[]) => void;
};

const ValidationConfig = (props: ValidationConfigProps) => {
const { session, onValidate } = props;
const [profile, setProfile] = React.useState<string>('netex');
const [schema, setSchema] = React.useState<string>('netex');
const [rules, setRules] = React.useState<string[]>(['frame_defaults', 'stop_point_names']);
const [fileList, setFileList] = React.useState<{ [key: string]: any }>({});
const [canValidate, setCanValidate] = React.useState<boolean>(false);
const apiClient = useApiClient();
Expand All @@ -85,7 +86,15 @@ const ValidationConfig = (props: ValidationConfigProps) => {
return;
}

setProfile(event.target.value);
setSchema(event.target.value);
};

const handleSelectMultChange = (event: SelectChangeEvent) => {
if (!event.target || !event.target.name || !Array.isArray(event.target.value)) {
return;
}

setRules(event.target.value);
};

const updateFileContext = (fileContext: any) => {
Expand Down Expand Up @@ -143,11 +152,11 @@ const ValidationConfig = (props: ValidationConfigProps) => {
</ul>
</Typography>
<FormControl>
<InputLabel id="netex-profile-label">Schema</InputLabel>
<InputLabel id="netex-schema-label">Schema</InputLabel>
<Select
labelId="netex-profile-label"
name="netex-profile"
value={profile}
labelId="netex-schema-label"
name="netex-schema"
value={schema}
onChange={handleSelectChange}
>
<MenuItem key="netex" value="netex">NeTEx</MenuItem>
Expand All @@ -156,6 +165,20 @@ const ValidationConfig = (props: ValidationConfigProps) => {
</Select>
</FormControl>

<FormControl>
<InputLabel id="rules-label">Rules</InputLabel>
<Select
labelId="rules-label"
name="rules"
value={rules as any}
multiple
onChange={handleSelectMultChange}
>
<MenuItem key="frame_defaults" value="frame_defaults">Frame defaults</MenuItem>
<MenuItem key="stop_point_names" value="stop_point_names">Stop point names</MenuItem>
</Select>
</FormControl>

<Typography variant="h4" sx={{ paddingTop: '10px' }}>Files</Typography>
<Typography gutterBottom><b>2.</b> Then select which files to validate by clicking &apos;Upload files&apos;</Typography>

Expand Down Expand Up @@ -214,7 +237,13 @@ const ValidationConfig = (props: ValidationConfigProps) => {
</Box>
)}

<Button variant="contained" disabled={!canValidate} onClick={() => onValidate(profile)}>Validate</Button>
<Button
variant="contained"
disabled={!canValidate}
onClick={() => onValidate(schema, rules)}
>
Validate
</Button>
</Stack>
</Stack>
</Stack>
Expand Down
4 changes: 2 additions & 2 deletions app/pages/jobs/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const Job: NextPage = () => {
const router= useRouter();
const apiClient = useApiClient();

const handleValidate = (schema: string) => {
const handleValidate = (schema: string, rules: string[]) => {
if (!session) {
return;
}

setSession({ ...session, status: 'running' });

apiClient.validate(session.id, schema)
apiClient.validate(session.id, schema, rules)
.then(session => setSession(session))
.catch(err => {
console.log('error caught running validation', err);
Expand Down
25 changes: 22 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var (
}
)

type ValidationQuery struct {
SessionID string `param:"sid"`
Schema string `query:"schema"`
Rules []string `query:"rules"`
}

type ServerConfig struct {
Port string
MQTTPort string
Expand Down Expand Up @@ -130,7 +136,12 @@ func StartServer(cfg *ServerConfig) {
})

e.GET("/api/sessions/:sid/validate", func(c echo.Context) error {
session := sessions.Get(c.Param("sid"))
query := ValidationQuery{}
if err := c.Bind(&query); err != nil {
return err
}

session := sessions.Get(query.SessionID)
if session == nil {
return fmt.Errorf("session not found")
} else if session.Status != "created" {
Expand All @@ -156,7 +167,7 @@ func StartServer(cfg *ServerConfig) {
file.File.Seek(0, 0)
}

schema := c.QueryParam("schema")
schema := query.Schema
if schema == "" {
schema = "netex"
}
Expand All @@ -170,9 +181,17 @@ func StartServer(cfg *ServerConfig) {
schema = "xsd/epip/NeTEx_publication_EPIP.xsd"
}

scriptingPaths := []string{"builtin/xsd.js"}
if query.Rules != nil {
for _, rule := range query.Rules {
scriptingPaths = append(scriptingPaths, "builtin/"+rule+".js")
}
}

validator, err := NewValidator(
WithSchemaFile(schema),
WithBuiltinScripts(true),
WithBuiltinScripts(false),
WithScriptingPaths(scriptingPaths),
)
if err != nil {
session.Stopped = time.Now()
Expand Down

0 comments on commit 80d9563

Please sign in to comment.