bbotella commented on code in PR #3904:
URL: https://github.com/apache/cassandra/pull/3904#discussion_r1958355949


##########
src/java/org/apache/cassandra/cql3/constraints/IsJsonConstraint.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.constraints;
+
+import java.nio.ByteBuffer;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.serializers.MarshalException;
+import org.apache.cassandra.utils.JsonUtils;
+
+import static java.lang.String.format;
+
+public class IsJsonConstraint extends ConstraintFunction
+{
+    public static final String FUNCTION_NAME = "IS_JSON";
+
+    public IsJsonConstraint(ColumnIdentifier columnName)
+    {
+        this(columnName, FUNCTION_NAME);
+    }
+
+    public IsJsonConstraint(ColumnIdentifier columnName, String name)
+    {
+        super(columnName, name);
+    }
+
+    @Override
+    public void internalEvaluate(AbstractType<?> valueType, Operator 
relationType, String term, ByteBuffer columnValue)
+    {
+        String cqlString = valueType.getString(columnValue);
+
+        try
+        {
+            JsonUtils.decodeJson(cqlString);
+        }
+        catch (MarshalException ex)
+        {
+            throw new ConstraintViolationException(format("Value for column 
'%s' violated %s constraint as it is not a valid JSON.",
+                                                          
columnName.toCQLString(),
+                                                          FUNCTION_NAME));
+        }
+    }
+
+    @Override
+    public void validate(ColumnMetadata columnMetadata) throws 
InvalidConstraintDefinitionException
+    {
+        if (!columnMetadata.type.unwrap().isString())
+            throw new InvalidConstraintDefinitionException(FUNCTION_NAME + " 
can be used only for columns of 'text' or 'ascii' types.");

Review Comment:
   And varchar as well?



##########
src/java/org/apache/cassandra/cql3/constraints/IsJsonConstraint.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.constraints;
+
+import java.nio.ByteBuffer;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.serializers.MarshalException;
+import org.apache.cassandra.utils.JsonUtils;
+
+import static java.lang.String.format;
+
+public class IsJsonConstraint extends ConstraintFunction

Review Comment:
   It is beautiful to see how easy it is to add a new constraint <3



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to