Search in sources :

Example 1 with Repeated

use of io.github.ihongs.util.verify.Repeated in project HongsCORE by ihongs.

the class TestVerify method testVerify.

@Test
public void testVerify() throws IOException {
    // 避免路径缺失致写入项目主目录
    CmdletRunner.init(new String[] { "--COREPATH", "target" });
    try {
        Verify verify = new Verify().addRule("name", new Required(), new IsString().config(Synt.mapOf("minlength", 3, "maxlength", 8))).addRule("size", new Repeated().config(Synt.mapOf("slice", ",")), new IsNumber().config(Synt.mapOf("min", 10, "max", 99)));
        Map values = Synt.mapOf("name", "abcdef", "size", "11,22");
        Map cleans = verify.verify(values, false, false);
        Map cleanz = Synt.mapOf("name", "abcdef", "size", Arrays.asList(11, 22));
        // System.out.println(Dawn.toString(cleans));
        // System.out.println(Dawn.toString(cleanz));
        assertEquals(Dawn.toString(cleans, true), Dawn.toString(cleanz, true));
    } catch (Wrongs wr) {
        Core.ACTION_LANG.set(Cnst.LANG_DEF);
        fail(Dawn.toString(wr.getErrors()));
    }
}
Also used : IsNumber(io.github.ihongs.util.verify.IsNumber) Required(io.github.ihongs.util.verify.Required) Wrongs(io.github.ihongs.util.verify.Wrongs) IsString(io.github.ihongs.util.verify.IsString) Repeated(io.github.ihongs.util.verify.Repeated) Verify(io.github.ihongs.util.verify.Verify) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Repeated

use of io.github.ihongs.util.verify.Repeated in project HongsCORE by ihongs.

the class VerifyHelper method addRulesByForm.

public VerifyHelper addRulesByForm(String conf, String form, Map fs) throws HongsException {
    FormSet formSet;
    formSet = FormSet.getInstance("default");
    Map ts = formSet.getEnum("__types__");
    Map ps = formSet.getEnum("__typos__");
    Iterator it = fs.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry et = (Map.Entry) it.next();
        String name = (String) et.getKey();
        if ("@".equals(name)) {
            continue;
        }
        Map optz = (Map) et.getValue();
        Map opts = new HashMap(optz);
        String ruls = (String) opts.remove("__rule__");
        opts.put("__conf__", conf);
        opts.put("__form__", form);
        opts.put("__name__", name);
        if (ruls == null || !ruls.startsWith("@")) {
            Object o;
            o = opts.get("defiant");
            if (o != null) {
                Rule rule = new Defiant();
                rule.config(opts);
                this.addRule(name, rule);
            }
            o = opts.get("default");
            if (o != null) {
                Rule rule = new Default();
                rule.config(opts);
                this.addRule(name, rule);
            }
            o = opts.remove("__required__");
            if (Synt.declare(o, false)) {
                Rule rule = new Required();
                rule.config(opts);
                this.addRule(name, rule);
            } else {
                Rule rule = new Optional();
                rule.config(opts);
                this.addRule(name, rule);
            }
            o = opts.remove("__repeated__");
            if (Synt.declare(o, false)) {
                Rule rule = new Repeated();
                rule.config(opts);
                this.addRule(name, rule);
            } else {
                Rule rule = new Ordinary();
                rule.config(opts);
                this.addRule(name, rule);
            }
        }
        String[] list;
        if (ruls == null || (ruls.length() == 0)) {
            String type = (String) opts.get("__type__");
            String item;
            if (ts.containsKey(type)) {
                // 类名转换
                item = (String) ts.get(type);
                String c = item.substring(0, 1);
                String n = item.substring(1);
                item = "Is" + c.toUpperCase() + n;
            } else if (ps.containsKey(type)) {
                // 类型正则
                opts.put("pattern", type);
                item = "IsString";
            } else {
                item = "IsString";
            }
            list = new String[] { item };
        } else {
            if (ruls.startsWith("@")) {
                ruls = ruls.substring(1);
            }
            list = ruls.split("[,;]");
        }
        // 添加规则实例
        for (String item : list) {
            item = item.trim();
            if (item.length() == 0) {
                continue;
            }
            if (!item.contains(".")) {
                item = Rule.class.getPackage().getName() + "." + item;
            }
            Rule rule;
            try {
                rule = (Rule) (Class.forName(item).getDeclaredConstructor().newInstance());
            } catch (ClassNotFoundException ex) {
                throw new HongsException(1125, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (NoSuchMethodException ex) {
                throw new HongsException(1125, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (InstantiationException ex) {
                throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (IllegalAccessException ex) {
                throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (IllegalArgumentException ex) {
                throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (InvocationTargetException ex) {
                throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (ClassCastException ex) {
                throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            }
            rule.config(opts);
            this.addRule(name, rule);
        }
    }
    /**
     * 唯一键约束
     */
    Map fp = (Map) fs.get("@");
    if (fp != null) {
        String fn;
        Object uk;
        Object ut;
        ut = fp.get("data-ut");
        if (ut == null) {
            ut = fp.get("data-at");
            if (ut == null) {
                ut = conf + "/" + form + "/search";
            }
        }
        for (Object o : fp.entrySet()) {
            Map.Entry e = (Map.Entry) o;
            fn = e.getKey().toString();
            if (fn.startsWith("data-uk-")) {
                fn = "UK-" + fn.substring(8);
            } else if (fn.equals(/**/
            "data-uk")) {
                fn = "UK";
            } else {
                continue;
            }
            uk = e.getValue();
            Rule fr = new IsUnique(true).config(Synt.mapOf("__name__", fn, "data-ut", ut, "data-uk", uk));
            this.addRule(fn, fr);
        }
    }
    return this;
}
Also used : HashMap(java.util.HashMap) Repeated(io.github.ihongs.util.verify.Repeated) Defiant(io.github.ihongs.util.verify.Defiant) Required(io.github.ihongs.util.verify.Required) IsUnique(io.github.ihongs.util.verify.IsUnique) Iterator(java.util.Iterator) Ordinary(io.github.ihongs.util.verify.Ordinary) Optional(io.github.ihongs.util.verify.Optional) Default(io.github.ihongs.util.verify.Default) InvocationTargetException(java.lang.reflect.InvocationTargetException) HongsException(io.github.ihongs.HongsException) Rule(io.github.ihongs.util.verify.Rule) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Repeated (io.github.ihongs.util.verify.Repeated)2 Required (io.github.ihongs.util.verify.Required)2 Map (java.util.Map)2 HongsException (io.github.ihongs.HongsException)1 Default (io.github.ihongs.util.verify.Default)1 Defiant (io.github.ihongs.util.verify.Defiant)1 IsNumber (io.github.ihongs.util.verify.IsNumber)1 IsString (io.github.ihongs.util.verify.IsString)1 IsUnique (io.github.ihongs.util.verify.IsUnique)1 Optional (io.github.ihongs.util.verify.Optional)1 Ordinary (io.github.ihongs.util.verify.Ordinary)1 Rule (io.github.ihongs.util.verify.Rule)1 Verify (io.github.ihongs.util.verify.Verify)1 Wrongs (io.github.ihongs.util.verify.Wrongs)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Test (org.junit.Test)1