001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bean.jsonschema;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.commons.bean.*;
024import org.apache.juneau.marshall.collections.*;
025
026/**
027 * {@link PropertyValidatorFactory} implementation backed by the typed {@link JsonSchema} bean.
028 *
029 * <p>
030 * This factory is registered as a {@link ServiceLoader ServiceLoader} provider for
031 * {@link PropertyValidatorFactory} via {@code META-INF/services/org.apache.juneau.commons.bean.PropertyValidatorFactory}.
032 * Marshalling-side code discovers it through {@link PropertyValidators#factory()} at runtime.
033 *
034 * <p>
035 * The factory converts the supplied JSON-Schema-shaped map into a {@link JsonSchema} bean via
036 * {@link JsonSchemaBeanGenerator#toBean(JsonMap)} and wraps it in a {@link JsonSchemaValidator}.
037 *
038 * @see JsonSchemaValidator
039 * @see PropertyValidatorFactory
040 * @since 10.0.0
041 */
042public final class JsonSchemaPropertyValidatorFactory implements PropertyValidatorFactory {
043
044   /** Public no-arg constructor required by {@link ServiceLoader ServiceLoader}. */
045   public JsonSchemaPropertyValidatorFactory() {
046      /* intentionally empty — required public no-arg constructor for ServiceLoader */
047   }
048
049   @Override
050   public PropertyValidator create(Map<String,Object> schemaMap, Class<?> propertyType) {
051      if (ie(schemaMap))
052         return null;
053      var jm = (schemaMap instanceof JsonMap schemaMap2) ? schemaMap2 : new JsonMap(schemaMap);
054      var schema = JsonSchemaBeanGenerator.toBean(jm);
055      return JsonSchemaValidator.of(schema);
056   }
057}