예제 #1
0
func validatorTask(c *config.Config, q *registry.Queue) error {
	output, err := utils.Exec("rm", []string{"-rf", "../app/lib/Validators"})
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("cannot remove original validators: %s", err)
	}

	rootPath := "../app/validators"
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		// Relative path
		rel, err := filepath.Rel(rootPath, path)
		if err != nil {
			return fmt.Errorf("cannot rel validator path: %s", err)
		}

		// Read file
		f, err := yaml.ReadFile(path)
		if err != nil {
			return fmt.Errorf("read validator failed: %s", err)
		}
		data := config.NewConfig(f)

		// Extract fields
		root := data.GetDefault("root", "Object")
		if root != "Object" && root != "Array" {
			return fmt.Errorf("invalid root type, only 'object' and 'array' are accepted")
		}
		fields := parseFields(data, "fields")

		// Generate validator
		if err := generator(rel, root, fields); err != nil {
			return fmt.Errorf("generator error: %s", err)
		}

		return nil
	}
	if err := filepath.Walk(rootPath, walkFn); err != nil {
		return fmt.Errorf("walk validators failed: %s", err)
	}

	return nil
}
예제 #2
0
파일: form.go 프로젝트: ernestoalejo/cb
func parseForm(filename string) (*formInfo, error) {
	f, err := yaml.ReadFile(filepath.Join("..", filename))
	if err != nil {
		return nil, fmt.Errorf("read form file failed: %s", err)
	}
	data := config.NewConfig(f)

	form := &formInfo{
		Filename:   filename,
		Name:       data.GetDefault("formname", "f"),
		Submit:     data.GetDefault("submitfunc", "submit"),
		TrySubmit:  data.GetDefault("trySubmitfunc", "trySubmit"),
		ObjName:    data.GetDefault("objname", "data"),
		Validators: make(map[string][]*validators.Validator),
	}

	nfields := data.CountDefault("fields")
	for i := 0; i < nfields; i++ {
		name := data.GetRequired("fields[%d].name", i)

		field, err := fields.Parse(data, i)
		if err != nil {
			return nil, fmt.Errorf("parse field failed for `%s`: %s", name, err)
		}
		if field != nil {
			form.Fields = append(form.Fields, field)
		}

		validators := validators.Parse(data, i)
		if validators != nil {
			form.Validators[name] = validators
		}
	}

	return form, nil
}