001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.requests.validators;
019
020import io.stallion.exceptions.ClientException;
021import io.stallion.reflection.PropertyUtils;
022
023import java.util.List;
024
025import static io.stallion.utils.Literals.*;
026
027/**
028 * Validates a bunch of variables and raises an informative exception if any are invalid.
029 *
030 * Example usage:
031 *
032 * {@literal @}POST
033 * public String newThing({@literal @}BodyParam("email") String email, {@literal @}BodyParam("name") String name, {@literal @}BodyParam String nickName) {
034 *     new ParamsValidator()
035 *         .add("name", name, 1)
036 *         .addEmail("email", email)
037 *         .optional("nickName", nickName)
038 *         .validate();
039 *
040 * }
041 *
042 *
043 *
044 */
045@Deprecated
046public class ParamsValidator {
047    List<OneParam> params = list();
048
049    public ParamsValidator add(String name, Object value) {
050        params.add(new OneParam().setName(name).setValue(value).setMinLength(1));
051        return this;
052    }
053
054    public ParamsValidator add(String name, Object value, int minLength) {
055        params.add(new OneParam().setName(name).setValue(value).setMinLength(minLength));
056        return this;
057    }
058
059    public ParamsValidator optional(String name, Object value) {
060        if (value == null || (value instanceof String && "".equals(value))) {
061            return this;
062        }
063        params.add(new OneParam().setName(name).setValue(value));
064        return this;
065
066    }
067
068    public ParamsValidator addEmail(String name, String value) {
069        params.add(new OneParam().setName(name).setValue(value).setEmail(true));
070        return this;
071    }
072
073    public <T> T toObject(Class<? extends T> cls) {
074        validate();
075        T o = null;
076        try {
077            o = cls.newInstance();
078            for(OneParam p: params) {
079                PropertyUtils.setProperty(o, p.getName(), p.getValue());
080            }
081        } catch (InstantiationException e) {
082            throw new RuntimeException(e);
083        } catch (IllegalAccessException e) {
084            throw new RuntimeException(e);
085        }
086
087        return o;
088    }
089
090
091    public void validate() {
092        List<String> errors = list();
093        for (OneParam p: params) {
094            if (p.getValue() == null) {
095                errors.add("Field " + p.getName() + " is required.");
096                continue;
097            }
098            if (p.isEmail()) {
099                String email = (String)p.getValue();
100                if (email.length() < 3) {
101                    errors.add("Field " + p.getName() + " must be a valid email address.");
102                }
103                if (!email.contains("@")) {
104                    errors.add("Field " + p.getName() + " must be a valid email address.");
105                }
106                continue;
107            }
108            if (p.getValue() instanceof String) {
109                String val = (String)p.getValue();
110                if (p.getMinLength() > 0 && val.length() == 0) {
111                    errors.add("Field " + p.getName() + " is required.");
112                } else if (val.length() < p.getMinLength()) {
113                    errors.add("Field " + p.getName() + " is too short.");
114                }
115                continue;
116            }
117        }
118        if (errors.size() > 0) {
119            throw new ClientException(String.join("<br>\n", errors));
120        }
121    }
122
123    static class OneParam {
124        private String name;
125        private Object value;
126        private int minLength = 1;
127        private boolean email = false;
128
129        public String getName() {
130            return name;
131        }
132
133        public OneParam setName(String name) {
134            this.name = name;
135            return this;
136        }
137
138        public Object getValue() {
139            return value;
140        }
141
142        public OneParam setValue(Object value) {
143            this.value = value;
144            return this;
145        }
146
147        public int getMinLength() {
148            return minLength;
149        }
150
151        public OneParam setMinLength(int minLength) {
152            this.minLength = minLength;
153            return this;
154        }
155
156        public boolean isEmail() {
157            return email;
158        }
159
160        public OneParam setEmail(boolean email) {
161            this.email = email;
162            return this;
163        }
164
165    }
166}